Page 1 of 1

Checkbox Tutorial Rate Topic: -----

#1 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3030
  • View blog
  • Posts: 10,553
  • Joined: 08-August 08

Posted 12 February 2009 - 10:13 AM

I've noticed a few people have been having problems with checkboxes, so I put this together:
<?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)


This post has been edited by CTphpnwb: 16 February 2009 - 08:53 PM


Is This A Good Question/Topic? 3
  • +

Replies To: Checkbox Tutorial

#2 mikxxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-November 08

Posted 24 February 2009 - 06:15 AM

Thanks for this one, I need this for my reservation form. :)
I hope this will work on my form.
Thanks thanks thanks. :)

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3030
  • View blog
  • Posts: 10,553
  • Joined: 08-August 08

Posted 04 May 2011 - 01:30 PM

Hmm, something happened to my code format above!
<?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;
?>

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,319
  • Joined: 08-June 10

Posted 11 May 2011 - 02:40 PM

Note: $_SERVER['php_self'] is not safe if used like that. a malicious user can insert HTML code that will open an XSS vulnerability. see also http://www.mc2design...fe-alternatives (from http://www.google.co..._self+security)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1