Solved

Cascade Drop Down

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.NET AJAX callbacks to web service are becoming very popular and unavoidable these days. The feel that the user gets when only a part of the page is updated without even hindering the UI is the advantage that the callbacks offer. The wait for the…
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 …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now