Dynamic Drop Down Lists using PHP/MySQL/AJAX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • George Best
    New Member
    • Feb 2011
    • 1

    Dynamic Drop Down Lists using PHP/MySQL/AJAX

    Hi everyone,

    I am trying to make three dynamic drop down lists. When I say dynamic I mean when I choose a value from the first one, the second one changes options and when I choose a value from the second one then the third one changes options. But at the end I want the to keep the selected values because I will submit them. For example the first list may be Country, the second one may be City and the third one Postal Code. So once I choose country I want the cities in that country to be visible and again once I choose city I want postal code to be visible, buy I do not want to loose my selections every time because I will submit them at the end. I was able to make it up to two drop down menus and it works perfect, but once I add the third everything is destroyed.
    Take a look at my code for the first two menus and if you can help me I will really appreciate it

    Code:
    <!doctype html public "-//w3c//dtd html 3.2//en">
    
    <html>
    	<head>
    		<title>Multiple drop down list box from plus2net</title>
    		<SCRIPT language=JavaScript>
    			function reload(form)
    			{
    			var val=form.cat.options[form.cat.options.selectedIndex].value;
    			self.location='dropmenutest.php?cat=' + val ;
    			}
    		</script>
    	</head>
    
    	<body>
    		<?php
    			require_once("../../dbconnection.php");
    		?>
    		
    		<?php
    			@$cat=$_GET['cat'];
    			if(strlen($cat) > 0 and is_numeric($cat))
    			{ // to check if $cat is numeric data or not. 
    			echo "Data Error";
    			exit;
    			}
    
    // Getting the data from Mysql table for first list box
    			$quer2=mysql_query("SELECT DISTINCT PARK FROM PARK");
    			
    // for second drop down list we will check if category is selected else we will display all the subcategory//
    			if(isset($cat) and strlen($cat) > 0){
    			$test="SELECT DISTINCT Room FROM Rooms where AREA = '";
    			$test = $test . $cat . "'";
    			$quer=mysql_query("$test"); 
    			}else{$quer=mysql_query("SELECT DISTINCT Room FROM Rooms"); } 
    
    			echo "<form method=post name=f1 action='dd-check.php'>";
    // Add your form processing page address to action in above line. Example  action=dd-check.php
    // Starting of first drop downlist
    			echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
    			while($noticia2 = mysql_fetch_array($quer2)) { 
    			if($noticia2['PARK']==@$cat){echo "<option selected value='$noticia2[PARK]'>$noticia2[PARK]</option>"."<BR>";}
    			else{echo  "<option value='$noticia2[PARK]'>$noticia2[PARK]</option>";}
    			}
    			echo "</select>";
    
    //Starting of second drop downlist
    			echo "<select name='subcat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";
    			while($noticia = mysql_fetch_array($quer)) { 
    			echo  "<option value='$noticia[Room]'>$noticia[Room]</option>";
    			}
    			echo "</select>";
    			
    //Starting of the third drop downlist
    			echo "<select name='subcat'><option value=''>Select one</option>";
    			while($noticia = mysql_fetch_array($quer)) { 
    			echo  "<option value='$noticia[Room]'>$noticia[Room]</option>";
    			}
    			echo "</select>";
    
    			
    //Add your other form fields as needed here
    			echo "<input type=submit value=Submit>";
    			echo "</form>";
    		?>
    	
    		<?php
    			require("../../disconnect.php")
    		?>	
    	</body>
    </html>
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    If you dont understand AJAX

    you can follow this example

    In your source file you can simply create the SELECT drop down list. and in ajax reply you can add the return in the proper value as HTML.

    Comment

    • dgreenhouse
      Recognized Expert Contributor
      • May 2008
      • 250

      #3
      You are not using Ajax in your code example.

      If you want to use your existing code; which would be simpler than Ajax, you'll need to send the value(s) of all of the DropDown (Combobox) elements with each reload.

      Code:
      This:
      # var val=form.cat.options[form.cat.options.selectedIndex].value;
      #             self.location='dropmenutest.php?cat=' + val ;
      
      Should change to something like this:
      
      # populate vars
      val1=form.cat.options[form.cat.options.selectedIndex].value;
      val2=form.subcat1.options[form.subcat1.options.selectedIndex].value;
      val3=form.subcat2.options[form.subcat2.options.selectedIndex].value;
      
      self.location='dropmenutest.php?cat=' + val1 + '&subcat1=' + val2 + '&subcat2=' + val3 ;
      Then your PHP code should test for the existence and validity of all of the arguments and variably perform the sql queries.

      i.e.
      1- if cat1 is invalid; reset all dropdowns
      2- if cat1 is valid and subcat1 and subcat2 are both invalid; keep the value of cat1, perform sql on cat1, and populate subcat1, subcat2's value is reset
      3- if cat1 is valid and subcat1 is valid; keep the values of cat1 and subcat1, perform sql on subcat1, and populate subcat2
      4- if cat1, subcat1, and subcat2 are valid; complete whatever logic is appropriate.

      Comment

      • candy89
        New Member
        • Apr 2012
        • 1

        #4
        You can simply do this by using jquery .ajax(). Just send the first drop-down value with onChange() function and fetch the data from db and display the response. You can find a well explained example here:

        Comment

        • Bharat383
          New Member
          • Aug 2011
          • 93

          #5
          Code:
          <html>
          	<head>
          		<script type="text/javascript">
          				var http = false;
          				if(navigator.appName == "Microsoft Internet Explorer")
          				{
          					http = new ActivexObject("Microsoft.XMLHTTP");
          				}
          				else
          				{
          					http = new XMLHttpRequest();
          				}
          				
          				function get_city(str)
          				{
          						if(str == "India")
          						{
          								var city_list = "<select name='city' onChange='get_zipcode(this.value)'><option value='Mumbai'>Mumbai</option><option value='Delhi'>Delhi</option><option value='Kolkata'>Kolkata</option></select>";
          						}
          						if(str == "Japan")
          						{	
          							var city_list = "<select name='city' onChange='get_zipcode(this.value)'><option value='japan_city1'>Japan City 1</option><option value='japan_city2'>Japan City 2</option><option value='japan_city3'>Japan City 3</option></select>";
          						}
          						if(str == "USA")
          						{	
          							var city_list = "<select name='city' onChange='get_zipcode(this.value)'><option value='usa_city1'>usa City 1</option><option value='usa_city2'>usa City 2</option><option value='usa_city3'>USA City 3</option></select>";
          						}						
          						document.getElementById("city_area").innerHTML=city_list;
          				}
          
          				function get_zipcode(str1)
          				{
          				
          						/* India city selection */
          						if(str1 == "Mumbai")
          						{
          								var zipcode_list = "<select name='zipcode' ><option value='123456'>123456</option><option value='123123'>123123</option><option value='978871'>978871</option></select>";
          						}
          						if(str1 == "Delhi")
          						{	
          								var zipcode_list = "<select name='zipcode' ><option value='000001'>000001</option><option value='000002'>000002</option><option value='00003'>00003</option></select>";
          						}
          						if(str1 == "Kolkata")
          						{	
          								var zipcode_list = "<select name='zipcode' ><option value='000004'>000004</option><option value='000005'>000005</option><option value='00006'>00006</option></select>";
          						}						
          						
          						/* Japan city selection */
          						if(str1 == "japan_city1")
          						{
          								var zipcode_list = "<select name='zipcode' ><option value='japanCode 1'>japanCode 1</option><option value='japanCode 2'>japanCode 2</option><option value='japanCode 3'>japanCode 3</option></select>";
          						}
          						if(str1 == "japan_city2")
          						{	
          								var zipcode_list = "<select name='zipcode' ><option value='9454545'>japan city code 2</option></select>";
          						}
          						if(str1 == "japan_city3")
          						{	
          								var zipcode_list = "<select name='zipcode' ><option value='99999'>99999</option><option value='88888'>888888</option><option value='666666'>666666</option></select>";
          						}						
          
          						/* USA city selection */
          						if(str1 == "usa_city1")
          						{
          								var zipcode_list = "<select name='zipcode' ><option value='11111'>11111</option><option value='j222222'>222222</option><option value='333333'>333333</option></select>";
          						}
          						if(str1 == "usa_city2")
          						{	
          								var zipcode_list = "<select name='zipcode' ><option value='13212121'>j1321321312</option></select>";
          						}
          						if(str1 == "usa_city3")
          						{	
          								var zipcode_list = "<select name='zipcode' ><option value='54545454'>454545</option><option value='21212121'>132132132</option><option value='854545'>8545421</option></select>";
          						}												
          
          						document.getElementById("zipcode_area").innerHTML=zipcode_list;
          				}
          				
          		</script>
          	</head>
          		<body>
          		<div id="m1"></div>
          					<form name="form1" method="post" >
          							<table>
          									<tr>
          											<th>Country</th>
          											<td>
          													<select name="country" id="country" onChange="get_city(this.value)">
          														<option value="0">Select Country</option>
          														<option value="India">India</option>
          														<option value="Japan">Japan</option>
          														<option value="USA">USA</option>
          													</select>
          											</td>
          									</tr>
          									<tr>
          											<th>City</th>
          											<td>
          													<div id="city_area">																									
          														<select name="city" id="city">
          																<option value="0">Select City</option>
          														</select>
          													</div>
          											</td>
          											
          									</tr>
          									<tr>
          											<th>Zipcode</th>
          											<td>
          												<div id="zipcode_area">																								
          													<select name="zip_code" id="zip_code">
          															<option value="0">Select Zip Code</option>
          													</select>
          												</div>
          											</td>
          									</tr>									
          							</table>
          					</form>
          		</body>
          </html>

          ===> it is only static as you can get ideas .... how to work it.....
          if you wanna change the values from database then make it with php and mysql

          Bharat Parmar(Bharat38 3)
          Last edited by Niheel; Apr 15 '12, 07:14 AM. Reason: Please use code tags and the code can be broken down into parts and explained better

          Comment

          Working...