School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,015 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,079 people online right now. Registration is fast and FREE... Join Now!




Guide to interactively use Select Options in a Drop Down Box

 
Reply to this topicStart new topic

> Guide to interactively use Select Options in a Drop Down Box

no2pencil
Group Icon



post 15 Oct, 2009 - 12:54 AM
Post #1


Guide to interactively use Select Options in a Drop Down Box

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 smile.gif

1st we have a simple Select/Option HTML block :
CODE

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

CODE

<?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 smile.gif Because we control the input (in our HTML value options) there is no need for a default. We know, without a doubt the value will be one two or three.

CODE

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

CODE
<?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. biggrin.gif icon_up.gif

This tutorial can be seen working at the following URL : http://akroncdnr.com/select.test.php
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 07:30AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month