Checkbox in Form
3 posts by 2 authors in: Forums > CMS Builder
Last Post: July 17, 2013 (RSS)
By depdesign - July 17, 2013
I am working on a form that applies info to a user account. Part of the form includes a PRE-CHECKED CHECKBOX that requires a user can un-check if they do not accept.
I have everything working fine, but, here is the issue:
If a user accepts the PRE-CHECKED CHECKBOX and submits then later decides to return to the form and unselect the PRE-CHECKED CHECKBOXand submit, the value in the account remains checked.
From what I've read in the posts is that a unselected checkbox does not send a value which is why its not overwriting the value to be 0.
Is there anything that can be added to the input field that would allow the value to be entered as 0 if a checkbox is unselected?
Here is the code used:
<input name="accept" value="1" checked="checked" type="checkbox" <?php if(@$_REQUEST['accept']){echo "checked='checked'";}?>>
By Daryl - July 17, 2013
Hi depdesign,
Try adding a hidden field with the same field name:
<input name="accept" value="0" type="hidden">
<input name="accept" value="1" checked="checked" type="checkbox" <?php if(@$_REQUEST['accept']){echo "checked='checked'";}?>>
How this works is if the checkbox is unchecked and submitted, the $_REQUEST['accept'] will still have a value of 0 coming from the value of the hidden field.
And if the checkbox is checked, it will overwrite that value to the value of the checkbox which is 1. Just make sure that the hidden field is on top of the checkbox.
Cheers,
Daryl
PHP Programmer - interactivetools.com