School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,097 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,045 people online right now. Registration is fast and FREE... Join Now!




Checkbox Tutorial

 
Reply to this topicStart new topic

> Checkbox Tutorial

CTphpnwb
Group Icon



post 12 Feb, 2009 - 09:13 AM
Post #1


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

This post has been edited by CTphpnwb: 16 Feb, 2009 - 07:53 PM


Attached File(s)
Attached File  checkbox.php ( 1.18k ) Number of downloads: 208
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

mikxxx
*



post 24 Feb, 2009 - 05:15 AM
Post #2
Thanks for this one, I need this for my reservation form. smile.gif
I hope this will work on my form.
Thanks thanks thanks. smile.gif
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 11:59AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month