Solved

Cascade Drop Down

Posted on 2009-04-08
4
245 Views
Last Modified: 2013-12-29
I need a quick solution to this;

I am making a form that has 3 drop downs. The first drop down you select region, the second and 3rd drop down gets populated with hotels in the respect regions.

It would help if no refresh is required or if we have the "select region first" for dropdown 2 and dropdown 3.
0
Comment
Question by:bernsoft
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 1

Accepted Solution

by:
PHPeZ earned 250 total points
ID: 24118454
Ok,
let me try to help:

Dropdowns on the form:
 - Have them in a container with unique id assigned. eg. <div> tags.
you might already have something like (i'm emphasizing on the functional part more):

<form............>
<div id="regionDiv">
<select name="regionDD" id="regionDD" onChange="populateHotel(this.value);">
<?php
   $query = mysql_query("SELECT `id`,`regionName` FROM `regionsTable`");
   while($obj=mysql_fetch_object($query))
   {
  ?>
   <option value="<?php echo $obj->id;?>"><?php echo $obj->regionName;?></option>
<?php
  }
 ?>
</select>
</div>

Similarly, you'd have dummy dropdowns for the hotel:
I'm doing just one dropdown, you could use it to create the third dropdown.

<div id="hotelDiv1">
<select name="hotelDD" id="hotelDD">
<option value="-1">--Select hotel--</option>
</select>
</div>

=============================================
The above would do for the interface. Now what you'd need is the function to populate the div with hotels.

I'm using Ajax below (referring to "no refresh is required"):
<script type="text/javascript">

function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var data = createObject();



function populateHotel(id)
{
            
var xmlhttp = getHTTPObject();
var file = 'getHotel.php?regionid='+id; //This is the path to the php file which has the code to output the hotels.*

xmlhttp.open('GET', file, true); //Open the file through GET, and add the page to retrieve as a GET variable **
xmlhttp.onreadystatechange=function()
      {
      
      if (xmlhttp.readyState==4) { //Check if it is ready to recieve data
         var content = xmlhttp.responseText; //The content data which has been retrieved ***
      
                  if(content){ //Make sure there is something in the content variable
      
                            document.getElementById('hotelDiv1').innerHTML = content;  //Change the inner content of your div to the newly retrieved content ****
      
                        }
                            }
      }
xmlhttp.send(null) //Nullify the XMLHttpRequest
return;
}
</script>
========================================================================
Now you'd have to have the file getHotel.php created and placed on the same folder as the above file is (note that i've written the ajax script on the same file as the dropdowns are, its better you create a separate .js file and use src attribute on <script> opening tag to make a call to the .js file).

Below is the code which should give you the hotels:
<?php
$regionId = $_GET[id]; // the id passed from the ajax script. :)
$queryHotel = mysql_query("SELECT `id`,`hotelName`  FROM `hotelsTable` WHERE `region`='".$regionId."'");
?>
<select name="hotelDD" id="hotelDD">
<option value="-1">--Select hotel--</option>
<?php
while($hotelObj=mysql_fetch_object($queryHotel))
{
?>
  <option value="<?php echo $hotelObj->id;?>"><?php echo $hotelObj->hotelName;?></option>
<?php
}
?>


=========
 I just did that on notepad..donno if I have some typos there, but I'm sure you'd fix it. The rest of the logic and code should work for you.

Cheers!
0
 

Author Comment

by:bernsoft
ID: 24121235
Thanks i will try this and get back to you.
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 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