0

我正在使用 php 中的 ajax 进行 stdying 下拉列表。我想加载基于第一个下拉列表的第二个下拉列表。

我写了下面的代码,但它给出的请求页面不在服务器错误上

<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>
4

1 回答 1

0

在您的代码中,选择标签没有id属性,但您正在使用

   var catcod = document.getElementById('catcod').value;

首先为选择标签提供id作为

  <select name="catcod" id="catcod" onchange="ajaxFunction()" >

并更改以下行(网址有不必要的间隙)

 ajaxRequest.open("GET", "ajax.php  ?catcod="+catcod, true); //find the gap

  ajaxRequest.open("GET", "ajax.php?catcod="+catcod, true);
于 2013-07-08T12:12:34.970 回答