Uniform - Checkbox behavior

hi fellaz!

right now i’m testing the different uniform fields.

        <label for="receive-copy">
            <input type="checkbox" name="_receive_copy" id="receive-copy" <?php e($form->value('_receive_copy'), ' checked')?>/> Receive a copy of the sent data

all fields work just fine… but… with the checkbox-field i get the
following error message via mail if the checkbox has not been checked:

<br />
<b>Notice</b>:  Undefined index: _receive_copy in <b>/home/url/www/dev.testing.net/site/snippets/uniform/contactform-mail.php</b> on line <b>15</b><br />

when the checkbox has been checked i get a correct
message like this:
_receive_copy: on

what do I miss?
thanx a lot for your help!

…and an additional question. :wink:
is it possible to check and transmit multiple checkbox values?
something like this:

    <input type="checkbox" name="flavours[]" value="strawberry">
    <input type="checkbox" name="flavours[]" value="vanilla">
    <input type="checkbox" name="flavours[]" value="chocolate">

how would this be handled by uniform?

saludos, funkybrotha

Please post the contents of your site/snippets/uniform/contactform-mail.php file.

It is. See here for an example. Also check out how the default snippets handle array values if you like to implement your own snippets.

hi @mzur!


…here the details:


echo l::get('age') . ': ' . $form['age'] . "\n";
echo l::get('salutation') . ': ' . $form['salutation'] . "\n";
echo l::get('name') . ': ' . $form['name'] . "\n";
echo l::get('surname') . ': ' . $form['surname'] . "\n";
echo l::get('street') . ': ' . $form['street'] . "\n";
echo l::get('postcode') . ': ' . $form['postcode'] . "\n";
echo l::get('phone') . ': ' . $form['phone'] . "\n";
echo l::get('email-address') . ': ' . $form['_from'] . "\n";
echo l::get('message') . ': ' . $form['message'] . "\n";
echo l::get('receive-newsletter') . ': ' . $form['newsletter'] . "\n";
echo l::get('favorite-composer') . ': ' . $form['fav-composer'] . "\n";

echo "receive_copy: {$form['_receive_copy']}" . "\n";

thanx for helping me out!


Please use Markdown code blocks when posting code. They use three backticks at start and end in separate lines. I have corrected it above.

The problem is caused by this line:

The state of a checkbox is only submitted with a form if it is checked. This means that there is no on or off if the checkbox was checked or not but there is either on or nothing at all. In the line above you assume that the _receive_copy key is always present in the form although it only is if the checkbox was checked, hence your “undefined index” error if it is not. To fix this line you could do something like this:

echo "receive_copy: " . array_key_exists('_receive_copy', $form) ? "on\n" : "off\n";

hi @lukasbestle!

thank you!

hi @mzur!

thanx a lot for your help!

now it works with this snippet! :wink:

echo 'receive_copy: ' . (array_key_exists('_receive_copy', $form) ? "on\n" : "off\n");