Creating pages from frontend - handling options or radio buttons

Currently I’m creating a contact form with data submited directly in kirby cms, this guide helped me a lot so now I have basic skeleton that’s working.

I’d like to add some select fields in form, like this:

<label for="sbausweis">Haben Sie einen Schwerbehindertenausweis oder Gleichsstellung?</label>
<select id="sbausweis" name="sbausweis">
  <option value="sb40">Schwerbehindertenausweis mit GdB von 40 oder weniger</option>
  <option value="sb50">Schwerbehindertenausweis mit GdB von 50 oder mehr</option>
  <option value="glst">Gleichsstellung</option>
  <option value="nosb">Weder SB-Ausweis noch Gleichsstellung</option>
</select>

I followed tutorial to add simple input fields, somethink like this in form

<label for="message">Ihr Anliegen</label>
<textarea name="message" id="message"><?= $data['message'] ?? null ?></textarea>

now I understand, the <?= $data['message'] ?? null ?> etc. is being handled here in controller:

    $data = [
        ...
        'email'   => get('email'),
        ...
    ];

Can someone help me how to/what I need to do to pass the data from select or radio to controller? :slight_smile:

In the same way with

get('sbausweis')

The difference is how you play the data back to the select field, because you have to check the value in the form to determine which option must get the selected attribute.

do you have maybe an example how to pass selected option value/data directly to cms?

Well, you are passing the $data array from the controller to the template, anyway. And then for every option, you check if the key is set and has the value of the current option:

<option value="sb40" <?= ($data['sbausweis'] ?? '' === 'sb40') ? 'selected' : '' ?>>Schwerbehindertenausweis mit GdB von 40 oder weniger</option>
1 Like

thanks being quick! :slight_smile: now I’ve adapted every option like in example above:

  <label for="sbausweis">Haben Sie einen Schwerbehindertenausweis oder Gleichsstellung?</label>
  <select id="sbausweis" name="sbausweis" form="sbausweis">
    <option value="sb40" <?= ($data['sbausweis'] ?? '' === 'sb40') ? 'selected' : '' ?>>Schwerbehindertenausweis mit GdB von 40 oder weniger</option>
    <option value="sb50" <?= ($data['sbausweis'] ?? '' === 'sb50') ? 'selected' : '' ?>>Schwerbehindertenausweis mit GdB von 50 oder mehr</option>
    <option value="glst" <?= ($data['sbausweis'] ?? '' === 'glst') ? 'selected' : '' ?>>Gleichsstellung</option>
    <option value="nosb" <?= ($data['sbausweis'] ?? '' === 'nosb') ? 'selected' : '' ?>>Weder SB-Ausweis noch Gleichsstellung</option>
  </select>

but nothing seems to be stored passed to .txt file

I need your complete controller, looks like you left something out…

It’s the same controller like in guide

return function ($kirby, $page) {

    // if the form has been submitted…
    if ($kirby->request()->is('POST') && get('register')) {

        // check the honeypot and exit if is has been filled in
        if(empty(get('website')) === false) {
            go($page->url());
            exit;
        }

        $data = [
            'name'    => get('name'),
            'vorname' => get('vorname'),
            'company' => get('company'),
            'sbausweis' => get('sbausweis'),
            'email'   => get('email'),
            'message' => get('message')
        ];

        $rules = [
            'name'  => ['required'],
            'email' => ['required', 'email'],
        ];

        $messages = [
            'name'  => 'Bitte geben Sie Ihren (link: #name text: Name)',
            'vorname'=> 'Bitte geben Sie Ihren (link: #vorname text: Vorname)',
            'email' => 'Bitte geben Sie Ihre (link: #email text: E-Mail Adresse)',
        ];

        // some of the data is invalid
        if ($invalid = invalid($data, $rules, $messages)) {
            $alert = $invalid;

        } else {

            // authenticate as almighty
            $kirby->impersonate('kirby');

            // everything is ok, let's try to create a new registration
            try {
                // we store registrations as subpages of the current page
                $registration = $page->createChild([
                    'slug'     => md5(str::slug($data['name'] . microtime())),
                    'template' => 'event.page.registration',
                    'content'  => $data
                ]);

                if ($registration) {
                    // store referer and name in session
                    $kirby->session()->set([
                        'referer' => $page->uri(),
                        'regName'  => esc($data['name'])
                    ]);
                    go('buchung-erfolgreich');
                }

            } catch (Exception $e) {
                $alert = ['Your registration failed: ' . $e->getMessage()];
            }
        }
    }

    // return data to template
    return [
        'alert' => $alert ?? null,
        'data'  => $data ?? false,
    ];
};

Hm, looks ok. Check if the key is present in $_POST and has a value, when you dump this in your template.

Hey Sonja,

actually, now it works! The mistake was on my side.

<select id="sbausweis" name="sbausweis" form="sbausweis">

the part form=“sbausweis” wasn’t identical with form class. Anyway thanks als always for nuddging to right direction :slight_smile: