I have successfully created some cascading dropdown lists, but I cannot seem to retrieve the values. The dropdown lists are entirely created on another page, and I don't know why, but I just assumed that it would pull the select name and selected value from the lists as well. When I print_r($_REQUEST), there is no information at all from the secondary and third dropdowns.
The first page is just a plain ol' form, like this:
<center><div id="country"><b>Country</b></div></center>
</td><td><center><div id="province"><b>Province</b></div></center>
</td><td>
This is the JavaScript
function showRecords(str,column,nextDiv)
{
if (str=="")
{
document.getElementById(nextDiv).innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById(nextDiv).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getRecords.php?"+column+"="+str,true);
xmlhttp.send();
}
This is the page that it is pulling dropdowns from
<?
require "functions.php";
if(isset($_REQUEST['region'])){
$region=$_REQUEST['region'];
getDepRecords("country","regions","region",$region,"province");
}
if(isset($_REQUEST['country'])){
$country=$_REQUEST['country'];
getDepRecords("province","countries","country",$country,"");
}
?>
This is the specific function
function getDepRecords($column, $table, $depColumn, $dep, $nextDiv) {
echo "<select name =".$column." id=".$column."
onchange=\"showRecords(this.value,'".$column."','".$nextDiv."')\">\n";
$options = "";
if (isset($_REQUEST)) {
$selected = $_REQUEST[$column];
}
$query = "SELECT DISTINCT $column FROM $table WHERE
$depColumn = \"$dep\" ORDER BY $column ASC";
$result = mysql_query($query);
if (!$result) {
$options = "<option>Error Retrieving Records</option>\n";
}
else {
$options.= "<option value = ".NULL.">Select ".$column."</option>\n";
$options.= "<option value = ".NULL."></option>\n";
while ($row = mysql_fetch_assoc($result)) {
$value = $row[$column];
$options.= "<";
$options.= "option value=\"";
$options.= $value . "\"";
if (isset($selected)) {
if (($selected) == ($value)) {
$options.= " selected";
}
$options.= "";
}
$options.= ">";
$options.= $value;
$options.= "</option>\n";
}
}
echo $options;
echo "</select>";
}
Your code may be incomplete, but from what there is, it appears the "plain old form" lacks submission information:
<td>
<center>
<div id="country"><b>Country</b></div>
</center>
</td>
<td>
<center>
<div id="province"><b>Province</b></div>
</center>
</td>
If you call the javascript function (for example)
showRecords("canada","country","province")
The select elements get inserted in the corresponding div, but there is no way that these can then be used without a form and an action.
Client HTML Client javascript web server mySQL
| | | |
|----------- get form page --------------->| |
|<--------- returns form page -------------| |
| | | |
|----- js event ----->|showRecords runs | |
| |-- getRecords.php ->| |
| | |---- SQL --->|
| | |<--- data ---|
| |<- return content --| |
|<--- data to user ---|ajax received | |
| | | |
|user submits form | | |
|----- request to where? POST or GET? ---->|process.php? |
| | | |
v v v v
If I'm missing the point, tell us more about your code.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.