Get values from checkboxes group

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 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']),
  ]
]);

Could you please post the form?

What do you get if you

dump(get('checkboxes'));

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

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
]);

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

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

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.