Solved

Cascade Drop Down

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

10 Experts available now in Live!

Get 1:1 Help Now