Jump to content

How to retrieve information from drop down menu ?


Jr0x

Recommended Posts

Hi, i'm a beginner php programmer.

I have something to ask regarding retrieving information from drop down menu.

Let's say i have a drop down menu in page1.php and i want to get/display the information on page2.php.

Dummy set of drop down list:

[code]
  <select name="select">
    <option value="a">Apple</option>
    <option value="o">Orange</option>
    <option value="b">Banana</option>
  </select>
[/code]

i tried using the same method as retrieving radiobutton. But it doesn't works.

Dummy code:

[code=php:0]
<?php
$fruit = $_POST['select'];

echo $fruit;

?>
[/code]

This is different from normal radio button where you just need to get the name
of the buttons. As all the name of the button are the same.
However, for drop down list, there is only 1 name in this case "select". But with different values.

Can i do something like,

[code=php:0]
<?php

if ($_POST['select'] && value = "a")
    echo "Apple";
elseif ($_POST['select'] && value = "o")
    echo "Orange";
elseif ($_POST['select'] && value = "b")
    echo "Banana";

?>
[/code]

Thanks for the help, because i tried a few method and couldn't work so have no choice but to seek for help.

Regards,
Jr0x
$blah = $_POST['select'];

That should yield the selected value of your drop down. (a, o, or b);

How you process that on the backend is up to you. if( $blah == 'a' ) { echo "Apple"; }

or you might consider:

<select name="fruit">
  <option value="Apple">Apple</option>
  <option value="Orange">Orange</option>
  <option value="Banana">Banana</option>
</select>

Then you could just say

echo $_POST['fruit'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.