In this Tutorial I will show you how to properly use the Select/Option HTML code in an interactive PHP environment. We will validate, & then properly utilize the users input. You can use $_GET interchangeably with $_POST in this tutorial. Their differences however, will not be discussed here.
Lets begin
1st we have a simple Select/Option HTML block :
<select name="dropdown"> <option value="1">First</option> <option value="2">Second</option> <option value="3">Third</option> </select>
We have three options, & for anyone who may not have seen, it's named the ever so obvious "dropdown". We will pull this value out of the $_POST array in PHP.
When someone is viewing the form, & the submit button is pushed, the value contain in "name" will be the array element in the $_POST array. So we could get away with doing something as simple as directly calling our passed in element.
<?php echo "<P>".$_POST['dropdown']."</p>"; ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <select name="dropdown"> <option value="1">First</option> <option value="2">Second</option> <option value="3">Third</option> </select> <input type="Submit" name="Submit"> </form>
However, this will simply print the numerical values given in the name column. While this will work great in a database, or for conditional comparisons, it does little to correspond with the viewer of our website. Since it's conveniently a numeric value, lets parse the value through a switch statement
<?php
$number=$_POST['dropdown'];
switch ($number) {
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="dropdown">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
<input type="Submit" name="Submit">
</form>
Now that's fine & dandy, but lets give the user a little more interaction, with a nice default message, urging them to make a selection. In the event that they don't choose a value, we can catch that by giving that option a value of zero, & verifying that the input value is not zero. By 1st checking for Submit, we know that the user has seen the options, has mad a selection, & we can now verify that the option was something other than default. The default value is prepared for them in the HTML option with the "selected" value.
<?php
if($_POST['Submit']) {
if($_POST['dropdown']==0) die("You must select a valid entry");
$number=$_POST['dropdown'];
switch ($number) {
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<select name="dropdown">
<option value="0">-Please choose one-</option>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>
<input type="Submit" name="Submit">
</form>
In a nutshell, this is a quick way to add a little interaction with your viewers when they are making a selection. It helps to give your form a little more personality, & take the faceless values away from your site.
This tutorial can be seen working at the following URL : http://akroncdnr.com/select.test.php





MultiQuote



|