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.
First off, your HTML is invalid or simply missing details.
You need a name element for your select box:
locationid won’t work when submitting a form:
You need a value attribute instead:
With the proper HTML, $_POST[‘lid’] will be the location id that you want. You can pass this to your function: