Link to home
Start Free TrialLog in
Avatar of bazconnolly
bazconnolly

asked on

populate 2nd drop down based on 1st drop down

Hi Thankyou both for your prompt response

I appoligise I should of explained there is only one mysql table called 'Resale' I cant change this and use a separate country table.

Here are the fields in the table I need to use:

Country , City

So I need to select the Country drop down which then populates the second city drop down automatically.

heres what I have got so far


<?php

$conn stuff here

$query = "SELECT DISTINCT Country FROM Resale ";
$result = mysql_query($query) or die ( ' error in query ' );


then populate the 1st drop down box

then need to refresh the page to populate the second city drop own box

$query = "SELECT DISTINCT City FROM Resale WHERE Country = (Country from 1st drop down)

please help

thanks
barry

ASKER CERTIFIED SOLUTION
Avatar of shobinsun
shobinsun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Better to use Ajax call to populate the second dropdown. Here is the code. So just download prototype.js from http://www.prototypejs.org/download
city.php
<?php
  $c=mysql_connect('localhost','root','');
  mysql_select_db('devel');
  $query = "SELECT DISTINCT City FROM Resale WHERE Country = '".$_GET['country']."'";
  $result = mysql_query($query) or die ( ' error in query ' );
  while($row = mysql_fetch_row($result)) echo '<option>'.$row[0].'</option>';
?>
index.php
<script src="prototype.js" type="text/javascript"></script>
<?php
  $c=mysql_connect('localhost','root','');
  mysql_select_db('devel');
  $query = "SELECT DISTINCT Country FROM Resale ";
  $result = mysql_query($query) or die ( ' error in query ' );?>
  <select onchange="new Ajax.Updater('city','http://ajax/city.php?country=' + this.options[this.selectedIndex].value)">;<?php
  while($row = mysql_fetch_row($result)) echo '<option>'.$row[0].'</option>';
  echo '</select>';
  echo '<select id="city"></select>';
?>
  

Open in new window

Avatar of bazconnolly
bazconnolly

ASKER

excllent service thankyou