Link to home
Start Free TrialLog in
Avatar of dessin
dessinFlag for United States of America

asked on

If checkbox checked then change checkbox value

Hello, I have a dynamic search page, based on user search criteria. I have 4 checkboxes. I want to change the checkbox value from 0 to 1, based on if the user checks the box. I am sure what I typed is not clear, so an example would be:

$v_label1 = 0;
<input type="checkbox" id="label1" value="'.$v_label1.'">Label 1<br />

But if the user checks the box, I want the value to change when I submit the search form to:
$v_label1 = 1;
<input type="checkbox" id="label1" value="'.$v_label1.'" checked>Label 1<br />
Avatar of hielo
Flag of Wallis and Futuna image

Strictly speaking, changing the value from 0 to 1 is not necessary because if the checkbox is not checked, the browser will not "send" the value of the checkbox, regardless of what value you have on the checkbox. So if you have this:
<input type="checkbox" name="label1" id="label1" value="Yes">Label 1<br />
AND the value is checked, on the server you can do this:
$v_label1 = false;
if( isset("label1") )
  $v_label = true;
Avatar of hielo
Flag of Wallis and Futuna image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dessin


Hielo answered and explained everything perfectly for me.