# Creating pages from frontend - handling options or radio buttons

**URL:** <https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043>\
**Category:** Questions\
**Tags:** v3\
**Created:** [July 22, 2022, 11:29am UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043 "2022-07-22T11:29:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![denkstream](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/denkstream/32/6628_2.png) [@denkstream](https://forum.getkirby.com/u/denkstream)\
**Post date:** [July 22, 2022, 11:29am UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/1 "2022-07-22T11:29:44Z")

</div>

Currently I’m creating a contact form with data submited directly in kirby cms, [this guide helped me a lot](https://getkirby.com/docs/cookbook/forms/creating-pages-from-frontend) 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? 🙂

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 22, 2022, 11:33am UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/2 "2022-07-22T11:33:27Z")

</div>

> [@denkstream](#):
>
> `sbausweis`

In the same way with

```auto
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.

---

<div class="post-metadata">

**Author:** ![denkstream](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/denkstream/32/6628_2.png) [@denkstream](https://forum.getkirby.com/u/denkstream)\
**Post date:** [July 22, 2022, 11:44am UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/3 "2022-07-22T11:44:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 22, 2022, 11:53am UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/4 "2022-07-22T11:53:50Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![denkstream](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/denkstream/32/6628_2.png) [@denkstream](https://forum.getkirby.com/u/denkstream)\
**Post date:** [July 22, 2022, 12:04pm UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/5 "2022-07-22T12:04:29Z")

</div>

thanks being quick! 🙂 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

 ![image](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/7/7e695e178297a4daa35935ef246e86e4fabb4e9c.jpeg)

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 22, 2022, 12:19pm UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/6 "2022-07-22T12:19:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![denkstream](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/denkstream/32/6628_2.png) [@denkstream](https://forum.getkirby.com/u/denkstream)\
**Post date:** [July 22, 2022, 12:21pm UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/7 "2022-07-22T12:21:17Z")

</div>

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,
    ];
};

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [July 22, 2022, 12:49pm UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/8 "2022-07-22T12:49:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![denkstream](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/denkstream/32/6628_2.png) [@denkstream](https://forum.getkirby.com/u/denkstream)\
**Post date:** [July 23, 2022, 1:20pm UTC](https://forum.getkirby.com/t/creating-pages-from-frontend-handling-options-or-radio-buttons/26043/9 "2022-07-23T13:20:15Z")

</div>

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 🙂
