Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get Drop Down 2 depends on drop down 1

Status
Not open for further replies.

maas

Programmer
Sep 3, 2009
148
BH
Hello All,

I am having difficulties while dealing with 2 drop down lists. The first drop down list fetches the values from DB about the country which has country name, code. The second drop down shows the concerned cities for the selection in drop down 1 which as country code, city code, city name.
I think the best ting to get these values is by ajax. So, can you please guide me.

Thanks
 
hello ggriffit,

Thanks for your help.

But, in my case I am retrieving the values from the DB, this will be different.

Can you please give me more information
 
You'll need to consider several things.

1. How you'll be sending the chosen value to the PHP script.

2. How your PHP page will be returning the values it gets from the database.


For the first one its either POST or GET are your choices. GET being the simpler one as you can just attach the value to the URL of the PHP script when you call it.

Code:
var myreq=new XMLHttpRequest();
myreq.open("GET","popdrop.php?myvalue=" + dropdwn.value,false);
myreq.send();
var results=myreq.responseText;


The XMLHttpRequest object is what allow the connection to a PHP script to retrieve the output of the script.

For Part 2
I would have my PHP script built to return either a Json array that my JS code can use to construct the new drop down, or simply return a string that you can split and manipulate lter.

An example of the string method follows:

Code:
$conn=mysql_connect(...) or die(mysql_error());
$db=mysql_select_db(...) or die(mysql_error());
[green]Assuming the request was done using the GET method, ou would want to search for the value from the dropdown in GET super global.  [/green]
$id=mysql_real_escape_string($_GET['cntry']); [green]Alwasy good to cleanse any variables[/green]
$qry="SELECT *FROM states WHERE Country_ID=$id";
[green]Build the query using the obtained value.[/green]
$res=mysql_query($qry);
$options="";
while($row=mysql_fetch_array($res)){

$options.=$row['id'] . "," . $row['name'] . "\n\r";
[green]Construct the string o Json array that wil be returned to the JS script[/green]
}

echo $options;
[green]Output, so the response cna be read by the Javascript code.[/green]
?>

Then its just a matter of manipulating the string or json array received in the JS part to build the dropdown.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Thanks vacunita,

I learned alot from this.

I am using jsp, so this will differ a little bit.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top