这是来自另一页的ajax数据:
<? $cat=strval($_GET['cat']);
//Connect to mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
$query="SELECT * FROM cats WHERE cat_id='$cat'";
$result=mysql_query($query);
?>
<select name="sub_cat">
<option>Select Sub-Category</option>
<? while($row2=mysql_fetch_array($result)) { ?>
<option value="<?=($row2['sub_cat_id'])?>"><?=$row2['sub_cat']?></option>
<? } ?></select>
这是显示在其上的页面:
<select name="cat" onChange="getSub_cat(this.value)">
<option>Select Category</option>
<?php
//Open database connection
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
die('Failed to connect to server: ' . mysql_error());
}
//Select database
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
die("Unable to select database");
}
//Select categories
$sql = "SELECT DISTINCT cat, cat_id FROM cats ";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option value=\"".$row['cat_id']."\">".$row['cat']."</option>\n ";
}
?>
</select>
<b>Sub-Category</b></td>
<div id="sub_catdiv"><select name="sub_cat" id="sub_cat"></div>
<option>Select Category First</option>
</select>
</div>
这是它传递的网址:
https://mysitewontworkcom/somepagephp?state=state&cat=cat&submit=submit
从ajax拉入表单的sub_cat根本没有显示在URL中!
“状态”(表格中较早)和“猫”拉得好吗? 有任何想法吗?请帮忙!
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getSub_cat(cat_id) {
var strURL="findCat.php?cat="+cat_id;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('sub_catdiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
答案 0 :(得分:1)
在显示数据的页面上,html结构出错:
<div id="sub_catdiv"><select name="sub_cat" id="sub_cat"></div>
<option>Select Category First</option>
</select>
更改为:
<div id="sub_catdiv">
</div>
正如您所看到的那样,您首先关闭div
而不是添加option
,而不是关闭似乎是主要问题的select
。