我已经成功创建了一些级联下拉列表,但我似乎无法检索这些值。下拉列表完全在另一个页面上创建,我不知道为什么,但我只是假设它将从列表中提取选择名称和选定值。当我print_r($ _ REQUEST)时,第二次和第三次下拉列表中根本没有信息。
第一页只是一个简单的形式,如下所示:
<center><div id="country"><b>Country</b></div></center>
</td><td><center><div id="province"><b>Province</b></div></center>
</td><td>
这是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();
}
这是从
拉出下拉菜单的页面<?
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,"");
}
?>
这是具体的功能
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>";
}
答案 0 :(得分:2)
您的代码可能不完整,但根据内容,似乎“普通旧表单”缺少提交信息:
<td>
<center>
<div id="country"><b>Country</b></div>
</center>
</td>
<td>
<center>
<div id="province"><b>Province</b></div>
</center>
</td>
如果你调用javascript函数(例如)
showRecords("canada","country","province")
select元素会插入到相应的div中,但是如果没有表单和动作,就无法使用它们。
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
如果我错过了这一点,请告诉我们有关您的代码的更多信息。