Page 1 of 1

passing appropriate values from one combo box to another

Posted: Wed Feb 15, 2012 7:16 am
by phpjawahar
Help me on this.
My need is,
when i select any state from the state combo box, its appropriate cityname must be displayed in the city combo box.

I have two tables

table 1
state
1. cityid
2. stateid
3. statename

table 2
city
1. stateid
2. cityid
3. cityname

posting my code.....

Code: Select all

<?php
error_reporting(0);
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("employee", $con);
$sel = "select stateid, statename from state";
$res = mysql_query($sel);
?>
<html>
<head>
<title>Loading value in combobox from DB</title>
</head>
<body>
<select name="state">
 <option value="">Select State</option>
 <?php
  while($row = mysql_fetch_array($res))
  {
 ?>
 <option value="<?=$row['stateid']?>"><?=$row['statename']?></option>
 <? } ?>
</select>
<p></p>
<?php
$sel_city = "select stateid, cityid, cityname from city where stateid='".$_REQUEST['state']."'";
?>
<select name="city_id">
 <option value="">select city</option>
 </select>
</body>
</html>
Should i want to create 'foreign key' for my 'stateid' and 'cityid'.
Advance thanks for your help.

Re: passing appropriate values from one combo box to another

Posted: Wed Feb 15, 2012 7:38 am
by G l a z z
You need to do it with javascript. Search on google for php mysql combobox

Re: passing appropriate values from one combo box to another

Posted: Wed Feb 15, 2012 8:31 am
by phpjawahar
Hi Glazz,

On searching in google with the keyword 'php mysql combo box', i receive many forums.
So, could you help me with exact website which give these coding.

With Thanks,
Jawahar

Re: passing appropriate values from one combo box to another

Posted: Wed Feb 15, 2012 11:13 pm
by Gopesh
Hi Try this http://roshanbh.com.np/2008/01/populate ... d-php.html
It uses ajax to populate the values onchange.Surely it will help u to understand the table structure.