Wafa
Wafa

Reputation: 53

Load values from database to create a drop down menu based on the selection from previous dropdown

My problem is that I try to load a drop down based on selection from previous drop down. I'm loading the values from database. but when I use $_get['emirate'] I'm not getting the value. It says the value is not set.

$data = mysql_query("SELECT * FROM emirate") 
        or die(mysql_error()); 

    Print "<table border cellpadding=3>"; 
    print"<tr><th><form method=get action='index.php'><select id=EMIRATE size=1><OPTION value=all>all</option>";
    while($info = mysql_fetch_array( $data )) 
    { 
        Print "<option value=". $info['em_name'] .">".$info['em_name']."</option>"; 
    }
    print"</select></form></th>";
    if(isset($_GET['EMIRATE'])){
        $name=$_GET['EMIRATE'];
        echo $name;

        $data = mysql_query("SELECT a_name FROM areas where em_id=(select em_id from emirate where em_name=\"".$name."\")") 
        or die(mysql_error());
        Print "<th><select name=AREAS size=1>";
        while($info = mysql_fetch_array( $data ))
        {
            Print "<option >".$info['a_name']."</option>";
        }
        print"</select></th>";
    }

Upvotes: 1

Views: 1362

Answers (3)

Wafa
Wafa

Reputation: 53

I found out the problem.

I haven't put a submit button and hence my form wasn't getting submitted. And hence I wasn't getting any value from $_GET.

http://php.net/manual/en/reserved.variables.get.php

Now I get the results smoothly.

Upvotes: 3

donald123
donald123

Reputation: 5739

<select id=EMIRATE size=1><OPTION value=all>all</option>

Please fix the HTML.... you miss to set the name attribute for your select...

<select id="EMIRATE" size="1" name="EMIRATE">
    <option value="all">all</option>
    ....

Upvotes: 1

which method are you using on your form on the previous page, post or get?

you can replace $_GET with $_REQUEST which retrieves values from both post and get.

Upvotes: 1

Related Questions