pTutorial

New way to learn PHP Example

PHP Pull down menu

Pull down menu with action

In this section we will learn, how to create pull down menus with the list of your favourite sites with the help of PHP (header function and simple array).

Pull down menu example

<?php

$favourite = array ("www.ptutorial.com",
"blogs.ptutorial.com",
"forums.ptutorial.com",
"www.google.com",
"www.bing.com");
?>

<form action = "" method="post">
<select name="demo">
<option  value = "10">Choose a site:</option>
<option  value = "0">pTutorial.com</option>
<option  value = "1">blogs.pTutorial.com</option>
<option  value = "3">forums.pTutorial.com</option>
<option  value = "3">google.com</option>
<option  value = "4">bing.com</option>

</select>
<input type="submit" value="Click to go!" name="jj">
</form>

<?php
if(isset($_POST['jj']))
{
$a=$_POST['demo'];
echo $b=$favourite[$a];	
header("Location: http://$b");
}

?>

Explanation

In order to create pull down menu, first create simple HTML form select option and submit button, the value of every option must be different according to your array.

For example, if you initialize an array with five elements, then option value start from zero to four.

After creating form, initialize array with your favourite site and pass the value of option into the array, you can make a variable or directly pass this into the header function as shown above. Isset function is used to check a variable is set or not.


QUOTES OF THE DAY

Crafty men condemn studies, simple men admire them, and wise use them.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER