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 Select List Object using PHP - The Dynamic checkbox object



Easily code dynamic PHP Select Lists

The Dynamic select list object

How to select, display and have fun with HTML select list objects using PHP and Super Globals

Select lists - aka 'drop down box' - are just html. Anybody can code them. Actually, select lists 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">


select list code goes in here between the form tags

</form>

Cut to the chase

ok, here is the code for a select list. When submitted the selected value is redisplayed:


<?php
// initialize or capture variable
$month = !isset($_POST['month'])? NULL : $_POST['month'];
?>
<select name="month">
<option value="<?php echo $month;?>" SELECTED><?php echo $month;?></option>
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<input type="submit" name="submit" value="Try Me!">
</form>

The orange text is the variable value, if selected and posted. The green text is what the visitor sees.

Can we really try it out?

  • select any month
  • submit and see the same choice again
<!-- START of monthForm.php -->
<!-- END of monthForm.php -->

A bit about the logic

The select list consists of start and end select tags, plus start and end option tags. The green text between the open and close option tag is meaningless, although it is what the visitor will see.

The option property called value contains the variable value that will be posted, if the option is picked.

If you want to force a specific option to be chosen (in a form with no PHP) then simply add the keyword SELECTED after the option value.

If you ARE using PHP then you may use the keyword SELECTED to echo out the currently selected value.

More properties

The select tag has additional properties you may use:
  • multiple - allows visitor to select more than one option
  • size - will display more than one option
The multiple property isn't dealt with here. The size property can be set like this:

size property

<select name="month" SIZE=13>

This is the effect of inserting a size value of 13 into our monthForm.php example:
We have that one extra option because twelve (12) are used, one for each month, and the thirteenth (13) is for the dynamic php option to show whatever was selected.

We're out of here

That pretty well wraps it up for now. Remember, the select tag property called name determines the variable name. The chosen value is posted as whatever option property value you set and the user selected. The beauty of this HTML object is the user has no control over what options are available. As a result, the integrity of submitted user data tends to be better with data submitted from a select list, rather than the possibly suspicious data submitted from a wide open text input type.

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)