Get checkbox value from frontend registration

Hi there!

I’m trying to get checkboxes value from the frontend page creation.

Following this "Creating pages from frontend " cookbook, I was able to use various type of html inputs (select, textarea, date, number, email) but checkboxes.

Surely i’m doing something wrong but dont find what.

Here’s the checkbox input :

<input
 type="checkbox"
 id="optionA" 
 value="<?= isset($data['optionA']) ? $data['optionA'] : '' ?>">

<input
 type="checkbox"
 id="optionB" 
 value="<?= isset($data['optionB']) ? $data['optionB'] : '' ?>">

The controller is exactly as explained in the guide :

'optionA'    => esc(get('optionA')),
'optionB'     => esc(get('optionB')), 

Thx in advance for any help.

The most obvious problem with your group of checkboxes is that they don’t have the same name:

<input
 type="checkbox"
 id="optionB" name="options[]"
 value="<?= isset($data['optionB']) ? $data['optionB'] : '' ?>">
<input
 type="checkbox"
 id="optionB" name="options[]"
 value="<?= isset($data['optionB']) ? $data['optionB'] : '' ?>">

The return value from these checkboxes will be an array, so you would have to address it as such in your data array.

Sorry texnixe but i don’t get it :confused:

In the cookbook there’s a different name and data for each input is the checkbox different than the others type ?

I tried to use one checkbox to simplify things but still no result inside the page.txt .

<input type="checkbox" id="standard" name="standard" value="<?= isset($data['standard']) ? $data['standard'] : '' ?>">

$data = array( 'standard' => esc(get('standard')) );

Well, a single checkbox is another thing than checkboxes, where the user may choose to check none, one or multiple options. From your question I took it you wanted to use checkboxes, not a single checkbox. And yes, checkboxes as well as radios are different in that all options need the same name to group them, and checkboxes give you an array, not a single value. So $data['standard'] should be an array (at least if you use name="standard[]". See also this SO topic: https://stackoverflow.com/questions/10655355/getting-a-checkbox-array-value-from-post

If you just use `name=“standard” without the brackets, you get a string like explained here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#Handling_multiple_checkboxes

Yes it is multiple (~10) options what I’m looking for sorry for the confusion, I thought that a single or multiple checkboxes were going to be the same and that cutting down the example to one options would helped me understand ^^’ …

I’ll take a close look at those two links, thanks a lot for your help.

Hi,
Great links, I understand more about array of checkboxes and was able to get their value on the registration form like that:

<?php
  if(isset($_POST['register'])){
    if (!empty($_POST['selection'])) {
      foreach($_POST['selection'] as $selection)
        {echo $selection;}
} }?>

But for the controller I’m a bit stuck finding how to store the selection[] array inside the data array, and save it on the register page.

I guess it’s more about php knowledge than kirby stuff, so I hope I’m not asking for too much, basically all of my php knowledge comes from making sites using kirby hehe :sweat_smile:.

Perhaps you have some other resources that could help me understand the $data = array() part inside the controller and how to save the checkbox array in it.

Again, thanks in advance for any help.

Théo G.

First of all, let’s escape all array elements and check that all values we get from the form are allowed; you can do so like this:

// callback that escapes each item
function escapeArray($item) {
  return esc($item);
}

// use array map to apply the callback to the array
$selection = array_map("escapeArray", $_POST['selection']);

//  array with all allowed values for $_POST['selection']
$allowedValues = ['a', 'b', 'c'];

// check if the values are a subset of the allowed values
if (!array_diff($selection, $allowedValues)) {
    //all is fine
    $selection = $selection;
}

// add to $data array

$data = [
      'firstname' => esc(get('firstname')),
      'lastname'  => esc(get('lastname')),
      // whatever data
      'selection' => $selection
    ];
1 Like