checkbox calculation with php

Asked By Theunis Lombard
07-Nov-09 12:25 PM
Earn up to 0 extra points for answering this tough question.
I'm rather new to php and i am stuck.
i want to have a single checkbox be calculated with php, which i partially get done, but when the checkbox is not selected, php does not recognize the cb1 index with $_POST["cb1"]. When the checkbox is selected, no errors are given.

  Re:

web mavin replied to Theunis Lombard
07-Nov-09 10:40 PM

Well I think thats a correct behavior..when your checkbox is checked it'd surely work fine. When checked, the variable name will contain the value of the checkbox. Look at the somewhat simimlar samples at the below links:-

http://www.globalissa.com/articles/articleCheckbox.php

http://www.smartwebby.com/PHP/Phptips2.asp

  You need to prefix your check condition with the ISSET function too.

[)ia6l0 iii replied to Theunis Lombard
08-Nov-09 01:26 PM
If your check box markup looks like:
<input type="checkbox" name="myCheckBox" value="Yes" />

The check box "mycheckbox" would have a value in the post array only if the checkbox is checked. Otherwise the value would be nothing.  so ensure that you check if the value is set and to checked. You can do it like:

<?php
if(isset($_POST['myCheckBox']) && $_POST['myCheckBox'] == 'Yes') 
{
   echo "checked.";
}
else
{
   echo "not checked";
}
?>
Create New Account