Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get value for unchecked checkbox in checkbox elements when form posted?

Tags:

arrays

forms

php

I have a form like below :

<form action="" method="post">
    <input type="checkbox" id="status_1" name="status_1" value="1" />
    <input type="checkbox" id="status_2" name="status_2" value="1" />
    <input type="checkbox" id="status_3" name="status_3" value="1" />
</form>

When i check all checkbox and post the form, the result is like this:

Array ([status_3] => 1 [status_2] => 1 [status_1] => 1 ) 

Then i uncheck second checkbox and post the form, the result is like this:

Array ( [status_3] => 1 [status_1] => 1 ) 

Is it possible to make result like this below when i uncheck second checkbox :

Array ( [status_3] => 1 [status_2] => 0 [status_1] => 1 ) 

There are ideas to do it?

like image 653
Fredy Avatar asked Oct 08 '13 05:10

Fredy


People also ask

What is the value of checkbox when unchecked?

Note: If a checkbox is unchecked when its form is submitted, there is no value submitted to the server to represent its unchecked state (e.g. value=unchecked ); the value is not submitted to the server at all.

How do you get a form to submit when a checkbox is checked?

If you need to submit a form when a checkbox is checked or when it is unchecked like when you are using a switch, a good way is to create an hidden input. If you try to submit the checkbox argument if the checkbox is unchecked the form will not be submitted at all.


2 Answers

First way - hidden fields (disadvantage: the user can manipulate the value of the field (but one can manipulate the value of the checkbox too, so it's not really a problem, if you only expect 1 or 0))

<form action="" method="post"> <input type="hidden" name="status_1" value="0" /> <input type="checkbox" id="status_1" name="status_1" value="1" /> <input type="hidden" name="status_2" value="0" /> <input type="checkbox" id="status_2" name="status_2" value="1" /> <input type="hidden" name="status_3" value="0" /> <input type="checkbox" id="status_3" name="status_3" value="1" /> <input type="submit" /> </form> <?php var_dump($_POST); /*  * checking only the second box outputs:  *   * array (size=3)   'status_1' => string '0' (length=1)   'status_2' => string '1' (length=1)   'status_3' => string '0' (length=1)  */ 

Second way - to assign default value for non-set indexes:

<form action="" method="post"> <input type="checkbox" id="status_1" name="status_1" value="1" /> <input type="checkbox" id="status_2" name="status_2" value="1" /> <input type="checkbox" id="status_3" name="status_3" value="1" /> <input type="submit" /> </form> <?php for($i = 1; $i<=count($_POST); $i++) {     $_POST["status_$i"] = isset($_POST["status_$i"]) ? $_POST["status_$i"] : 0; } var_dump($_POST);  /**  * Here we will be checking only the third checkbox:  *   * array (size=3)   'status_3' => string '1' (length=1)   'status_1' => int 0   'status_2' => int 0  */ 
like image 107
Royal Bg Avatar answered Sep 20 '22 13:09

Royal Bg


I think adding hidden fields like this will work

<input type="hidden" id="status_1_" name="status_1"  value="0">
<input type="checkbox" id="status_1" name="status_1" value="1" />

<input type="hidden" id="status_2_" name="status_2" value="0">
<input type="checkbox" id="status_2" name="status_2" value="1" />

<input type="hidden" id="status_3_" name="status_3" value="0">
<input type="checkbox" id="status_3" name="status_3" value="1" />
like image 23
nithin Avatar answered Sep 20 '22 13:09

nithin