Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Cascade Drop Down

Posted on 2009-04-08
4
Medium Priority
?
247 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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
There is basically two types of AJAX request in mootools. Request  and Request.HTML Request: Request is the basic XHR request class in MooTools. While not extremely useful on its own, it provides the basic functionality for both Request.HTM…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

705 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