<?php
$checkboxname = array("Item_Zero","Item_One","Item_Two","Item_Three","Item_Four"); // Array with check box names. Five items = five check boxes.
var_dump($_POST); // Here so that you can see the POST values, which depend on what is checked. Items unchecked will not be in POST.
$output = '';
$output .= '<form method="post" action=" '.$_SERVER['php_self'].'">
';
foreach ($checkboxname as $checkbox) {
if($_POST[$checkbox]=="on")
{
$checked = "checked"; // if box is checked, set $checked to "checked"
} else {
unset($checked); // if box is unchecked, set $checked to null.
}
$selection = $checkbox.", or any text you like.
"; // The text that the user sees can be anything. Here were using the checkbox name so that you can see that it matches up.
$output .= "<input type = 'checkbox' ".$checked." name= ".$checkbox." >".$selection."<br>"; // Having set the variables, we set the checkbox html.
} // This is the end of the loop. We either start over on the next box, or after the last one, we proceed to the submit button.
$output .= '<input type="submit" name="Submit" value="Click Here when boxes have been selected"></form><br>'; // This sets up the submit button.
echo $output;
?>
It is (hopefully) self explanatory. It simply strings together the html necessary to create a basic series of checkboxes, and then outputs them.
If you run the code, check off a box, click the "Click Here..." button, and then view the html source in your browser, you should see something like this:
array(2) {
["Item_One"]=>
string(2) "on"
["Submit"]=>
string(40) "Click Here when boxes have been selected"
}
<form method="post" action=" ">
<input type = 'checkbox' name= Item_Zero >Item_Zero, or any text you like.
<br><input type = 'checkbox' checked name= Item_One >Item_One, or any text you like.
<br><input type = 'checkbox' name= Item_Two >Item_Two, or any text you like.
<br><input type = 'checkbox' name= Item_Three >Item_Three, or any text you like.
<br><input type = 'checkbox' name= Item_Four >Item_Four, or any text you like.
<br><input type="submit" name="Submit" value="Click Here when boxes have been selected"></form><br>
Copy/paste messes with the tab characters. See attached file.
Attached File(s)
-
checkbox.php (1.18K)
Number of downloads: 1307
This post has been edited by CTphpnwb: 16 February 2009 - 08:53 PM






MultiQuote




|