473,511 Members | 16,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dependent dropdown list with mysql

16 New Member
Hi,
I have two dynamic drop down lists: State and City.
What I'm trying to do is first I need the city list to be empty on load. Then after a visitors selects the "state" from the first list, the page reloads and it shows all the available cities from my database where state is selected.
Here is what I have sofar:

Expand|Select|Wrap|Line Numbers
  1. <?php require_once('Connections/myConnection.php'); ?>
  2. <?php
  3. // Check for errors
  4. error_reporting(E_ALL);
  5. ini_set('display_errors', True);
  6.  
  7. //
  8.  
  9. if (!function_exists("GetSQLValueString")) {
  10. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
  11. {
  12.   $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  13.  
  14.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  15.  
  16.   switch ($theType) {
  17.     case "text":
  18.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  19.       break;    
  20.     case "long":
  21.     case "int":
  22.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  23.       break;
  24.     case "double":
  25.       $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  26.       break;
  27.     case "date":
  28.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  29.       break;
  30.     case "defined":
  31.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  32.       break;
  33.   }
  34.   return $theValue;
  35. }
  36. }
  37.  
  38. mysql_select_db($database_myConnection, $myConnection);
  39. $query_state_recordset = "SELECT DISTINCT `State` FROM programs ORDER BY `State` ASC";
  40. $state_recordset = mysql_query($query_state_recordset, $myConnection) or die(mysql_error());
  41. $row_state_recordset = mysql_fetch_assoc($state_recordset);
  42. $totalRows_state_recordset = mysql_num_rows($state_recordset);
  43.  
  44. mysql_select_db($database_myConnection, $myConnection);
  45. $query_city_recordset = "SELECT DISTINCT City FROM programs ORDER BY City ASC";
  46. $city_recordset = mysql_query($query_city_recordset, $myConnection) or die(mysql_error());
  47. $row_city_recordset = mysql_fetch_assoc($city_recordset);
  48. $totalRows_city_recordset = mysql_num_rows($city_recordset);
  49. ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  50. <html xmlns="http://www.w3.org/1999/xhtml">
  51. <head>
  52. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  53. <title>Untitled Document</title>
  54. </head>
  55.  
  56. <body>
  57. <form id="form1" name="form1" method="post" action="">
  58.   <label></label>
  59.   <p>
  60.     <label>State
  61.     <select name="state_id" id="state_id">
  62.       <option value=""></option>
  63.       <?php
  64. do {  
  65. ?>
  66.       <option value="<?php echo $row_state_recordset['State']?>"><?php echo $row_state_recordset['State']?></option>
  67.       <?php
  68. } while ($row_state_recordset = mysql_fetch_assoc($state_recordset));
  69.   $rows = mysql_num_rows($state_recordset);
  70.   if($rows > 0) {
  71.       mysql_data_seek($state_recordset, 0);
  72.       $row_state_recordset = mysql_fetch_assoc($state_recordset);
  73.   }
  74. ?>
  75.         </select>
  76.     </label>
  77.   </p>
  78.   <p>
  79.     <label>City
  80.     <select name="city_ig" id="city_ig">
  81.       <option value=""></option>
  82.       <?php
  83. do {  
  84. ?>
  85.       <option value="<?php echo $row_city_recordset['City']?>"><?php echo $row_city_recordset['City']?></option>
  86.       <?php
  87. } while ($row_city_recordset = mysql_fetch_assoc($city_recordset));
  88.   $rows = mysql_num_rows($city_recordset);
  89.   if($rows > 0) {
  90.       mysql_data_seek($city_recordset, 0);
  91.       $row_city_recordset = mysql_fetch_assoc($city_recordset);
  92.   }
  93. ?>
  94.         </select>
  95.     </label>
  96.   </p>
  97. </form>
  98. </body>
  99. </html>
  100. <?php
  101. mysql_free_result($state_recordset);
  102.  
  103. mysql_free_result($city_recordset);
  104. ?>
  105.  
The page can be found HERE

Thanks
Sep 15 '07 #1
2 11078
ak1dnar
1,584 Recognized Expert Top Contributor
There were some threads for this. Please use site search tool to find them. Search in "Php forum" with keywords "dependent drop down"
Good Luck!
-ajaxrand
Sep 15 '07 #2
ak1dnar
1,584 Recognized Expert Top Contributor
[Refresh and filter record set]
Thread Title changed to better describe the problem scenario.
Please read here: Use a Good Thread Title
Sep 15 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
495
by: Tony Grider | last post by:
I am trying to find a way to create dependent dropdown lists in C# The old way I knew involved javascript and will no longer work in the new design. I need for each list to be database driven (data...
1
1390
by: Hifni Shahzard | last post by:
Hi, I'm using VS.NET 2003, SQL Server 2000, Windows xp professional; My Question: Some of you might seen in some websites where it requests the Country in a dropdown. If the Country selected,...
2
3224
by: computer_angel | last post by:
can someone help me pls.... how to get the value from the mysql when using two drop down that is dependent to the first? For example....(online enrolment) department then the other dropdown...
3
1625
by: riyaz | last post by:
dear mvps i am a beginner for asp.net ,i want to produce the data in the datagrid based upon the selection from the dropdown list. plz give me some code for me to develop this regards...
11
7360
by: eureka | last post by:
Hi All, I'm training in Servlets, JSP and JavaScript, I have a web page in which there's a "StudentName" textbox and below it is a "Names" Dropdown list. Initially the Textbox is empty and...
2
2449
by: sailajapotha | last post by:
I have one javascript code with me,to show parent dropdown as well as child dropdown. <html> <head> <script type="text/javascript" language="javascript"> function setOptions(chosen) { var...
48
2348
by: coool | last post by:
Hi I'm now trying to have a dependent lists in a form my form is a query based form i.e. I fill my MySQL query from this form I have around 30 fields/columns
5
1858
by: Bels | last post by:
I am currently designing a real estate website using Dreamweaver CS3, php, MySQL on a Mac. On it is a 'property search' form that queries the property database. The form consists of 5 dropdown lists....
1
2023
by: kanimozhiz | last post by:
Hi, I am using dropdown lists. What I need is on the selection of one item in one dropdown list, the second dropdown list should get populated accordingly. can anyone tell me how to do this in C#...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7423
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.