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?
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))
Second way - to assign default value for non-set indexes:
Assuming we are using checkboxes with zeros or ones...
Using a hidden checkbox with a zero value is just a work-around. Another work around would be to add 0 to the value when receiving the post or get.
Example:
$chkbx1 = $_POST['chckbx1']; $chkbx1 += 0;
This takes a NULL value an turns it into a zero, but if the value is one, as in its checked, then the value stays the same.
The real issue here isn't inventing a work-around. Its understanding why this is happening. Older versions of mySQL takes null values and converts them into a zero. In newer versions, you must disable strict mode and then a work-around is not needed.
I thinks it impossible to get array like what you want from html forms. But this some tricks can be used:
Result:
the question may already be answered but i just wanted to take a stab at it...server side only solution:
Testing
Thanks all. Thank to @RoyalBg give me solution. Like this :
It's work perfectly.. :)