How Can I Get The DropDown List value on Next Page ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
saqib389
Forum Commoner
Posts: 44
Joined: Wed Nov 30, 2005 2:13 am

How Can I Get The DropDown List value on Next Page ?

Post by saqib389 »

here is the code of

page1.php

Code: Select all

<html>
<head>
<title>My Page</title>
</head>
<body>
<form name="myform" action="display.php">
<div align="center">
<select name="mydropdown">
<option value="color1">black</option>
<option value="color2">red</option>
<option value="color3">Orange</option>
</select>
</div>
</form>
</body>
</html>
now i want to display on DISPLAY.PHP
the value of that which user selected in dropdownlist
like user select red option. then on next page there shuld b display RED and also there shuld b display RED COLOR.......
can any one tell me how can i dot this ?
Deemo
Forum Contributor
Posts: 418
Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC

Post by Deemo »

AFAIK you can only get the values from the drop down menu. Meaning if the user selected red, $_POST['mydropdown'] would return color 2. I dont know if its possible to get the text as well, but usually only the value is important
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

on display.php

Code: Select all

//simplest way
//would fire error if mydropdown was not set
echo $_POST['mydropdown'];

//better way
echo (!empty($_POST['mydropdown']) ? $_POST['mydropdown'] : 'My Drop Down was not set');
If you actually want it to display RED, then your going to have to change your form

Code: Select all

<option value="black">black</option>
<option value="red">red</option>
<option value="orange">Orange</option>
or you can do something like

Code: Select all

$colors = array(
   'color1' => 'BLACK',
   'color2' => 'RED',
   'color3' => 'ORANGE'
);

//check if mydropdownmenu exists
$color = (!empty($_POST['mydropdownmenu']) ? $_POST['mydropdownmenu'] : 'color1');
//check if its an acceptable value
if (in_array($color, $colors))
   echo $colors[$color];
//if not display default
else
   echo $colors[1];

I suggest you take a read http://ca.php.net/variables.predefined
saqib389
Forum Commoner
Posts: 44
Joined: Wed Nov 30, 2005 2:13 am

Post by saqib389 »

Thx... I gOT mY aNSWER..... clap for You :)
Post Reply