Solved

populate 2nd drop down based on 1st drop down

Posted on 2009-03-31
3
903 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:bazconnolly
3 Comments
 
LVL 14

Accepted Solution

by:
shobinsun earned 500 total points
ID: 24027264
Hello,

Build some arrays in javascript that list the possible options, then fill the second dropdown with one of those arrays based on the selection in the first dropdown.

For more details, go through the links (an example):

http://www.dhtmlgoodies.com/index.html?whichScript=ajax_chained_select

http://www.dhtmlgoodies.com/scripts/ajax-chained-select/ajax-chained-select.html



http://teamtutorials.com/web-development-tutorials/pulling-drop-down-values-from-a-database-using-javascript-and-php

http://www.zazzybob.com/onchange.html

http://www.javascriptkit.com/jsref/select.shtml

Hope these links will help you.

Regards.
0
 
LVL 4

Expert Comment

by:Fugas
ID: 24027370
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

0
 

Author Closing Comment

by:bazconnolly
ID: 31564743
excllent service thankyou
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem with Simple PHP/mySQL Query 3 52
wamp versus xampp 4 47
PHP: Insert Data into MySQL 5 19
array_values - reorder after unset? 5 14
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now