Solved

Cascade Drop Down

Posted on 2009-04-08
4
232 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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I am discussing AJAX problems in IE7 and I bet this will helps many guys out here who have problems with AJAX work. Lets start with the discovery of problem and then we will talk about its different solutions. My last two projects included …
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

713 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