|
|
|
| Home Products Download Support News Purchase About |
|
|
Easily code dynamic PHP checkboxesThe Dynamic checkbox objectHow to check, uncheck, display and have fun with HTML checkbox objects using PHP and Super GlobalsCheckboxes are just html. Anybody can code them. Actually, checkboxes belong somewhere between the open form element, and the close form element. The open form element should have at least a name, method and action properties and optionally an enctype property for file/image uploads. Just like this:<form name="myForm" method="post" action="someScript.php"> Given that our checkboxes are somewhere between the form open and close, checkboxes are intended for use when more than one selection is possible. For example, if you were subscribing to a list of online magazines, then you should have checkboxes for folks to subscribe to as many magazines as they like. Here are three fictitious magazines: Geek World Online Hawaiian Surfer Boring Accounting Obviously you would not want to receive the 'Boring Accounting' magazine, so you would not check that checkbox. If you submit the form - magazineForm.php - the checkboxes were on you would submit the value of 'on' for the name of the checkbox you checked. If you did not check the box then the submitted value will be 'off'. A little code will make this clearer: Checkbox codeHere's the checkbox code for the above magazines:<input type="checkbox" name="Geek_World_Online_Magazine"> Geek World Online How to make the checkbox already checkedIf you want to have a checkbox already ticked then you just put the word 'CHECKED' at the end of the html, within the checkbox html. The following checkbox is like this, it is already checked:<input type="checkbox" name="Geek_World_Online_Magazine" CHECKED> Geek World Online The above code snippet produces this actual effect: And the submitted value is ...When you click the submit button the submitted value for checked boxes will be 'on'. Checkboxes that are not checked will be 'off'. If we submitted the 'Geek_World_Online_Magazine' CHECKED input as above then the variable named '$Geek_World_Online_Magazine' will have a value of 'on'. OK, that's great, how do we show that same value again if we need to?Just ask the visitor to click the back button?Not. Say the visitor checked the magazines they wanted, but they had a mistake in their email address, so they had to complete the form again. Not! It's much better just to show the form again - include("magazineForm.php") - and populate it with the chosen values. How?Populating a checkbox with a PHP determined valueInitialize or captureWith checkboxes all you do is test for the value of the variable. If the value is 'on' then you add the word 'CHECKED' to the html. We now use superglobals to extract and set values for variables. This is one of several ways to capture or initialize a value for a variable - keep in mind the value may be sent via $_GET, $_POST, $_REQUEST, $_COOKIE or other way (read manual) :// initialize or capture the value $Geek_World_Online_Magazine = !isset($_POST["Geek_World_Online_Magazine"]? NULL: $_POST["Geek_World_Online_Magazine"]);The effect of this code is to retrieve the value for the variable. Our condition tests if a variable is set for 'Geek_World_Online_Magazine'. If this variable does not exist (a set variable with no value would cause our condition to evaluate to true) then place a value of NULL into $Geek_World_Online_Magazine. Otherwise, grab the value resulting from a post only. Ternary operatorThe Ternary operator may seem like a veil of obscurity, but after a few years its a thing of beauty:$variable = ( condition ) ? "value if condition is true" : "value if condition is false" ;
... so now we have initialized or captured the posted value of 'on' for the variable named $Geek_World_Online_Magazine. Now we show that value: Show the captured valueThe code looks like this:
<input type="checkbox" name="Geek_World_Online_Magazine" <?php if($Geek_World_Online_Magazine == "on"){echo " CHECKED";}?>> Geek World Online
The code should produce this:Geek World Online Sure, but let's REALLY try the post
And there you have seen the value for a checkbox variable, from the beginning to almost the end. Let's see the Source Code!
Sure, add another property to the checkboxMaking it more complicated than it has to beYou can also add the property of 'value' to checkboxes. With this you then have three object properties: type, name and value. The only difference is you test for the value you set.Example. If you set this value: <input type="checkbox" name="Geek_World_Online_Magazine" value="subscribe" CHECKED> ... then you would test for the value (after capturing the value), like:
<input type="checkbox" name="Geek_World_Online_Magazine" <?php
if($Geek_World_Online_Magazine == "subscribe"){echo " CHECKED";}?>> Geek World Online
That's it for one checkbox. Add as many more as you like, but be sure to keep the property called name different for each checkbox you have. Have fun! |
|||||
|
||||||