Forum Moderators: coopster

Message Too Old, No Replies

checkbox post as 1 or 0 instead of "on" or ""?

         

partha

1:12 am on Apr 20, 2005 (gmt 0)

10+ Year Member



When I submit a checkbox to a php script it, a checked checkbox seems to output the string "on" and an unchecked one outputs the string "".

Is there any way to get it to recognize as 1 or 0 without having php manually rewrite the var's value?

ironik

2:31 am on Apr 20, 2005 (gmt 0)

10+ Year Member



Set the html to:

<input type="checkbox" name="mycheckbox" value="1" />

If the checkbox has been ticked php will see $_POST['mycheckbox'] as having the value 1. If it hasn't been checked then PHP won't even know it exists. You normally use isset($_POST['mycheckbox']) to see whether a box has been ticked or not.

dcrombie

9:34 am on Apr 20, 2005 (gmt 0)



<?PHP 
# form processing
$mycheckbox = (isset($mycheckbox) && $mycheckbox == "on") ? 1 : 0;
?>