473,324 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Dependent dropdown list with mysql

16
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 11005
ak1dnar
1,584 Expert 1GB
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 Expert 1GB
[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
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
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
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
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
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
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
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
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
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
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.