i在php中使用ajax标准化下拉列表。我想在第一次投递的基础上加载第二次投递的下载列表。
我写了以下代码,但它给出的请求页面不在服务器错误上
<table style="font-family:Tahoma; font-size:18px;">
<tr><td>Category code:</td>
<td>
<select name="catcod" onchange="ajaxFunction()" >
<option value="00" selected="selected">--Select--</option>
<?php
$i=0;
while($catcod=mysql_fetch_array($cq))
{
print "<option value='"".$catcod ['cat_code']."'">".$catcod ['cat_desc']."</option>";
}?>
</select></td>
</tr>
<tr>
<td id="ajaxdiv">
</td>
</tr>
<tr><td>Item number:</td><td><input type="text" name="itemnum" id="itemnum"> </td></tr>
<tr><td>Description of Item:</td><td><input type="text" name="desc" id="desc"></td></tr>
<tr><td>Number of items:</td><td><input type="text" name="nom" id="nom"></td></tr>
<tr><td>Date of purchase:</td><td><input style="color:#888;" maxlength="6"type="text" name="date" id="date" value="DDMMYY"></td></tr>
<tr><td>Orginal cost:</td><td><input type="text" name="orgcost" id="orgcost"></td></tr>
<tr><td>Employee code:</td><td><input type="text" maxlength="6" name="emp" id="emp"></td></tr>
<tr>
<td colspan="1"><center><input type="submit" value="Update"></td>
</tr>
</table>
ajax代码如下
function ajaxFunction(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
}catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxdiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var catcod = document.getElementById('catcod').value;
ajaxRequest.open("GET", "ajax.php ?catcod="+catcod, true);
ajaxRequest.send(null);
}
ajax.php页面如下
<body>
<div class="window">
<div class="header">
<?php
include_once '../header.php';
?>
</div>
<?php
$catcode=$_GET['catcod'];
include_once '../includes/Configurations.php';
$link = mysql_connect($_dbConfig['dbServer'],$_dbConfig['dbUser'],$_dbConfig['dbPassword'])or die(mysql_error());
$db = mysql_select_db($_dbConfig['dbName'], $link) or die(mysql_error());
$catnum_query ="select cat_desc,cat_no from item_master where cat_code=$catcode";
$cat=mysql_query($catnum_query,$link);?>
<select name="catcnum">
<?php
while($catnum=mysql_fetch_array($cat))
{
print "<option value='"".$catnum ['cat_no']."'">".$catnum ['cat_desc']."</option>";
}?>
</select>
</div>
</body>
在您的代码中,select标签没有id属性,但您使用的是
var catcod = document.getElementById('catcod').value;
首先为提供id选择标签作为
<select name="catcod" id="catcod" onchange="ajaxFunction()" >
并更改下面的行(在url处有不必要的间隙)
ajaxRequest.open("GET", "ajax.php ?catcod="+catcod, true); //find the gap
至
ajaxRequest.open("GET", "ajax.php?catcod="+catcod, true);