# Get values from checkboxes group

**URL:** <https://forum.getkirby.com/t/get-values-from-checkboxes-group/17839>\
**Category:** Questions\
**Tags:** v3\
**Created:** [April 10, 2020, 10:29am UTC](https://forum.getkirby.com/t/get-values-from-checkboxes-group/17839 "2020-04-10T10:29:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexpi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/alexpi/32/12008_2.png) [@alexpi](https://forum.getkirby.com/u/alexpi)\
**Post date:** [April 10, 2020, 10:29am UTC](https://forum.getkirby.com/t/get-values-from-checkboxes-group/17839/1 "2020-04-10T10:29:29Z")

</div>

Hello,

I have a form that besides other things, contains a group of checkboxes (using a same ‘name’ attribute value).  
I used the [cookbook example](https://getkirby.com/docs/cookbook/forms/basic-contact-form) for the form, and everything works except the checkboxes. I understand that I have to get an array of the checkbox selection values somehow, but my php knowledge is very poor.

In the following code snippets I got everything from the cookbook example and I am trying to add the checkboxes:

```
// formhandler.php
$data = [
 'checkboxes' => get('checkboxes'),
];

$kirby->email([
  'from' => '...',
  'data' => [
    'checkboxes' => esc($data['checkboxes']),
  ]
]);

```

---

<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:** [April 10, 2020, 10:39am UTC](https://forum.getkirby.com/t/get-values-from-checkboxes-group/17839/2 "2020-04-10T10:39:19Z")

</div>

Could you please post the form?

What do you get if you

```auto
dump(get('checkboxes'));

```

Using `esc()` on an array doesn’t work…

---

<div class="post-metadata">

**Author:** ![alexpi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/alexpi/32/12008_2.png) [@alexpi](https://forum.getkirby.com/u/alexpi)\
**Post date:** [April 10, 2020, 10:50am UTC](https://forum.getkirby.com/t/get-values-from-checkboxes-group/17839/3 "2020-04-10T10:50:18Z")

</div>

The form contains many inputs of this type:

```
<label for="medication">Φαρμακευτική αγωγή
 <input type="checkbox" name="help" id="medication">
</label>

 <label for="checks">Διακριτικοί έλεγχοι
  <input type="checkbox" name="help" id="checks">
</label>

<label for="dementia">Αλτσχάιμερ / Άνοια
 <input type="checkbox" name="help" id="dementia">
</label>

```

I am using fetch() to send the form, and I haven’t used a controller. Where can I put the dump()?  
The complete form handler:

```
<?php

$alert = null;

if ($kirby->request()->is('POST')) {
  if (empty(get('website')) === false) {
    go($page->url());
    exit;
  }  

  $data = [
    'name' => get('name'),
    'sex' => get('sex'),
    'age' => get('age'),
    'email' => get('email'),
    'help' => get('help'), // these are the checkboxes
    'phone' => get('phone'),
    'message' => get('message')
  ];

  $rules = [
    'name' => ['required', 'min' => 3],
    'sex' => ['required'],
    'age' => ['required', 'min' => 67, 'max' => 100],
    'email' => ['required', 'email'],
    'phone' => ['required'],
    'message' => ['required', 'max' => 3000],
  ];     

  $messages = [
    'name' => 'Παρακαλούμε εισάγετε το όνομα σας',
    'sex' => 'Παρακαλούμε εισάγετε το φύλο σας',
    'age' => 'Παρακαλούμε εισάγετε μια έγκυρη ηλικία',
    'email' => 'Παρακαλούμε εισάγετε ένα έγκυρο email',
    'phone' => 'Παρακαλούμε εισάγετε το τηλέφωνο σας',
    'message' => 'Παρακαλούμε εισάγετε to μήνυμα σας'
  ];

  if ($invalid = invalid($data, $rules, $messages)) {
    $alert = $invalid;
  } else {
    try {
      $kirby->email([
        'template' => 'email',
        'from' => '...',
        'replyTo' => $data['email'],
        'to' => '...',
        'subject' => 'Νέα εκδήλωση ενδιαφέροντος',
        'data' => [
          'name' => esc($data['name']),
          'sex' => esc($data['sex']),
          'age' => esc($data['age']),
          'help' => esc($data['help']),
          'email' => esc($data['email']),
          'phone' => esc($data['phone']),
          'message' => esc($data['message'])
        ]
      ]);

    } catch (Exception $error) {
      $alert['error'] = "Η φόρμα δεν μπόρεσε να σταλεί";
    }

    if (empty($alert) === true) {
      $success = 'Το μήνυμα σας στάλθηκε με επιτυχία. Θα επικοινωνήσουμε μαζί σας σύντομα';
      $data = [];
    }
  }
}

echo json_encode([
  'alert' => $alert,
  'data' => $data ?? false,
  'success' => $success ?? 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:** [April 10, 2020, 11:32am UTC](https://forum.getkirby.com/t/get-values-from-checkboxes-group/17839/4 "2020-04-10T11:32:06Z")

</div>

To get an array of values from your checkboxes, you can change the name to `help[]`. But as I said, you then will get an array of values that you can’t treat like a simple string with the `ecs()` method, as `esc()` only takes a string as input. But you can turn it into a string with

```auto
esc(implode(',', get('help'))

```

---

<div class="post-metadata">

**Author:** ![alexpi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/alexpi/32/12008_2.png) [@alexpi](https://forum.getkirby.com/u/alexpi)\
**Post date:** [April 10, 2020, 11:42am UTC](https://forum.getkirby.com/t/get-values-from-checkboxes-group/17839/5 "2020-04-10T11:42:34Z")

</div>

Thank you very much! I was forgetting to put the brackets on help[], and I also needed to set the value attribute for each input, in order to get the actual values in the array.
