Software Powers the Net    Visit our family of sites:    phpyellow.com | globalissa.com | globalissa.net | phpwhite.com                  Contact
Software Powers the Net
Home     Products     Download     Support     News     Purchase     About    
HTML Checkbox Objects using PHP - The Dynamic checkbox object - A free tutorial from Globalissa.com



Easily code dynamic PHP checkboxes

The Dynamic checkbox object

How to check, uncheck, display and have fun with HTML checkbox objects using PHP and Super Globals

Checkboxes 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">


checkbox code goes in here between the form tags

</form>


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 code

Here's the checkbox code for the above magazines:


<input type="checkbox" name="Geek_World_Online_Magazine"> Geek World Online
<input type="checkbox" name="Hawaiian_Surfer_Magazine"> Hawaiian Surfer
<input type="checkbox" name="Boring_Accounting_Magazine"> Boring Accounting

How to make the checkbox already checked

If 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:
Geek World Online

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 value

Initialize or capture

With 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 operator

The 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" ;
  • condition may be any expression that resolves to true or false
  • value may be any string, or variable or expression that resolves to some value or NULL

... 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 value

The 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

  • check or uncheck any checkboxes
  • submit them and see the same choices again
<!-- START of magazineForm.php -->

Subscribe to Fictitious Magazines here

Geek World Online
Hawaiian Surfer
Boring Accounting
Jump to source code section
<!-- END of magazineForm.php -->


And there you have seen the value for a checkbox variable, from the beginning to almost the end.

Let's see the Source Code!

  • slightly scrambled source code for magazineForm.php - there should NOT be any wrapping!:

    <table bgcolor="honeydew">
        <tr>
            <td>
                <?php echo"&lt;!-- START of magazineForm.php --&gt;";?>
                <form name="magazineForm" method="post" action="articleCheckbox.php">
                    <input type="hidden" name="resultsPlease" value="Right Now!">
                    <h3>Subscribe to Fictitious Magazines here</h3>
                    <input type="checkbox" name="Geek_World_Online_Magazine"<?php if($Geek_World_Online_Magazine == "on"){echo" CHECKED";}?>> Geek World Online<br>
                    <input type="checkbox" name="Hawaiian_Surfer_Magazine"<?php if($Hawaiian_Surfer_Magazine == "on"){echo" CHECKED";}?>> Hawaiian Surfer<br>
                    <input type="checkbox" name="Boring_Accounting_Magazine"<?php if($Boring_Accounting_Magazine == "on"){echo" CHECKED";}?>> Boring Accounting<br>
                    <input type="submit" name="submit" value="Submit">
                </form>
                <a href="#source">Jump to source code section</a><br>
                <?php echo"&lt;!-- END of magazineForm.php --&gt;";?>
            </td>
        </tr>
    </table>

Sure, add another property to the checkbox

Making it more complicated than it has to be

You 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!

Home     Products     Download     Support     News     Purchase    
All content Copyright©2002-2008 Global I.S. S.A. Globalissa, phpYellow, Community News RSS, admin-Login-Only, acl-only, EasySQL and phpWhite are ™ trademarks of Global I.S. S.A. All rights reserved.



Validated by HTML Validator (based on Tidy)