|
|
|
| Home Products Download Support News Purchase About |
|
|
Easily code dynamic PHP Select ListsThe Dynamic select list objectHow to select, display and have fun with HTML select list objects using PHP and Super GlobalsSelect 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"> Cut to the chaseok, 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?
<!-- START of monthForm.php -->
<!-- END of monthForm.php -->
A bit about the logicThe 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 propertiesThe select tag has additional properties you may use:
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 hereThat 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! |
|||||
|
||||||