Submit an HTML form with empty checkboxes

Viewed 79376

I have an HTML form - with PHP, I am sending the data of the form into a MySQL database. Some of the answers to the questions on the form have checkboxes. Obviously, the user does not have to tick all checkboxes for one question. I also want to make the other questions (including radio groups) optional.

However, if I submit the form with empty boxes, radio-groups etc, I received a long list of 'Undefined index' error messages for each of them.

How can I get around this? Thanks.

8 Answers

I've used this technique from time to time:

<input type="hidden" name="the_checkbox" value="0" />
<input type="checkbox" name="the_checkbox" value="1" />

note: This gets interpreted differently in different server-side languages, so test and adjust if necessary. Thanks to SimonSimCity for the tip.

Unchecked radio or checkbox elements are not submitted as they are not considered as successful. So you have to check if they are sent using the isset or empty function.

if (isset($_POST['checkbox'])) {
    // checkbox has been checked
}

An unchecked checkbox doesn't get sent in the POST data. You should just check if it's empty:

if (empty($_POST['myCheckbox']))
     ....
else
     ....

In PHP empty() and isset() don't generate notices.

Use this

$myvalue = (isset($_POST['checkbox']) ? $_POST['checkbox'] : 0;

Or substituting whatever your no value is for the 0

To add to fmsf's code, when adding checkboxes I make them an array by having [] in the name

<FORM METHOD=POST ACTION="statistics.jsp?q=1&g=1">
    <input type="radio" name="gerais_radio" value="primeiras">Primeiras Consultas por medico<br/>
    <input type="radio" name="gerais_radio" value="salas">Consultas por Sala <br/>
    <input type="radio" name="gerais_radio" value="assistencia">Pacientes por assistencia<br/>
    <input type="checkbox" name="option[]" value="Option1">Option1<br/>
    <input type="checkbox" name="option[]" value="Option2">Option2<br/>
    <input type="checkbox" name="option[]" value="Option3">Option3<br/>
    <input type="submit" value="Ver">

We are trouble on detecting which one checked or not.

If you are populating form in a for loop, please use value property as a data holder:

    <?php for($i=1;$i<6;$i++):?>
    <input type="checkbox" name="active[]" value="<?php echo $i ?>"
    <?endfor;?>

If submit form you'll get order numbers of checkboxes that checked (in this case I checked 3rd and 4th checkboxes):

   array(1) {       
       ["active"]=>
          array(2) {
            [0]=>       
             string(1) "3"
            [1]=>
             string(1) "4"
          }
   }

When you are processing form data in loop, let's say in post.php, use following code to detect if related row is selected:

    if(in_array($_POST['active'] ,$i)) 
            $answer_result = true;
        else 
            $answer_result = false;

Final code for testing:

    <?php if (isset($_POST) && !empty($_POST)):
        echo '<pre>';
        var_dump($_POST);
        echo '</pre>';
    endif;
    ?>
    <form action="test.php" method="post">
    <?php for($i=1;$i<6;$i++):?>
    <input type="checkbox" name="active[]" value="<?php echo $i; ?>" />
    <?php endfor;?>
    <button type="submit">Submit</button>
    </form>
Related