I'm currently faced with a dilemma. I'm trying to build a menu that will display an array of data from a database. The user will then select an item from that list, and based on this, I need to make a database call to retrieve that data associated with that. Here is the current code that I have.
function foobar() {
$first_option_sql = "SELECT * from {location} l ORDER BY name ASC";
$list_first_option = db_query($first_option_sql);
$output .= "<p><b>Please select the primary location that you would like to check.</b><p>"
$output .= "<br><b>Location: </b> <select id='list'><option>Available locations</option>";
while($first_option_available = db_fetch_object($list_first_option)){
$output .="<option locationid='$first_option_available->lid'>".$first_option_available->name."</option>";
}
$output .="</select><input type=submit value='Search'>";
$output .="<p><b>Children: </b><select id='child'><option selected='yes'>Available children</option><p>";
$children_available_sql = "SELECT complexquery WHERE lid='%d' ORDER BY l.name ASC";
$list_children = db_query($children_available_sql);
while($children_available = db_fetcj_object($list_children)){
$output .="<option>".$children_available->name."</option>";
}
return $output;
}
The problem here is that using this code, am unable to retrieve the value of the selected "first option", thus the second query never actually taking place; this does not populate the second list.
I was thinking of using AJAX or Javascript, but I'm not too familiar with either. Therefore, I wanted some insight as to how I may be able to perform this.
onChangeof the first option list, and pass the value of the first select list as a POST or GET param. Then, at the head of your page, check if that paramisset. If it is, then you know the form was submitted, and you use that value to trigger the second select list lookup.