Page 1 of 1

Guide to interactively use Select Options in a Drop Down Box Rate Topic: -----

#1 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Posted 15 October 2009 - 01:54 AM

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 :)

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

<?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. :D :^:

This tutorial can be seen working at the following URL : http://akroncdnr.com/select.test.php

Is This A Good Question/Topic? 0
  • +

Replies To: Guide to interactively use Select Options in a Drop Down Box

#2 Guest_everythingis*


Reputation:

Posted 21 March 2010 - 03:30 PM

When using the switch statement, does that affect how the data is input into the database by renaming it "Number 1", etc. or is it still input correctly as a value of 01, 02, or 03 for a mysql formatted value?


Thanks!
Was This Post Helpful? 0

#3 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Posted 06 April 2010 - 09:13 PM

The values are passed over the form. The strings (for display) are pulled from the database & are for reference & readability.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1