Thursday, April 26, 2012

PHP, check if the checkbox is checked



Suppose we have a check box array demo[]
<input type="checkbox" name="demo[]" value="1" />A
<input type="checkbox" name="demo[]" value="2" />B
<input type="checkbox" name="demo[]" value="3" />C

To check if any checkbox is checked
   if(!isset($_POST['demo'])){
    echo "You need to check the checkboxs";
   }

 To access the checkbox values using:
$_POST['demo'][0],  $_POST['demo'][1], $_POST['demo'][2]

To make a default checked, for example in the second checkbox
 <input type="checkbox" name="demo[]" value="2" checked />

Result for demo:
A B C

No comments:

Post a Comment