PHP5中的Ajax - 使用其他下拉列表更新下拉列表

时间:2015-12-08 14:40:48

标签: php ajax

我尝试使用Ajax使用另一个下拉列表更新下拉列表。不知怎的,它不起作用。我不确定哪条路会出错。

php文件

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript">
    function AjaxFunction(cat_id)
    {
    var httpxml;
    try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
        try
                   {
                httpxml=new ActiveXObject("Msxml2.XMLHTTP");
                }
           catch (e)
                {
             try
            {
            httpxml=new ActiveXObject("Microsoft.XMLHTTP");
            }
             catch (e)
            {
            alert("Your browser does not support AJAX!");
            return false;
            }
          }
  }
function stateck() 
    {
    if(httpxml.readyState==4)
      {

var myarray=eval(httpxml.responseText);
for(j=document.testform.subcat.options.length-1;j>=0;j--)
{
document.testform.subcat.remove(j);
}


for (i=0;i<myarray.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myarray[i];
optn.value = myarray[i];
document.testform.subcat.options.add(optn);

} 
      }
    }
   var url="dd2.php";
url=url+"?cat_id="+cat_id;
url=url+"&sid="+Math.random();
httpxml.onreadystatechange=stateck;
httpxml.open("GET",url,true);
httpxml.send(null);
  }
</script>


<title>tresn</title>
</head>


<table width="1021" border="0" style="top:0;left:0; position:fixed">
  <tr>
    <td height="25"><form action='Test3.php?mula=0&amp;akhir=12' method="POST" class="dropdown_button" name="testform" id="form1">
      <strong> 
        Sort By:
        <select name="cat" onchange="AjaxFunction(this.value);" id="SortBy">
          <option value="0">(Short By)</option>
          <?php
$q=mysqli_query($db,"select * from db_prd_category ");
while($n=mysqli_fetch_array($q)){
echo "<option value=$n[Cat_Id]>$n[Name]</option>";
}
?>
        </select>
        <select name="subcat">
        </select>
        <input type="submit" class ="small button yellow" name="Filter" id="Filter" style="height:25px" value="Filter" />
        &nbsp; </strong>
    </form></td>
  </tr>
</table>`

Ajax文件

<?php
$cat_id=$_GET['cat_id'];
require "config.php";

$q=mysqli_query($db,"Select * from db_prd_subcategory where Cat_Id='$cat_id' ORDER BY  SubCat_Id ASC");
echo mysqli_error();
$myarray=array();
$str="";

while($n=mysqli_fetch_array($q)){
$str=$str . "\"$nt[Name]\"".",";
}

$str=substr($str,0,(strLen($str)-1)); 
echo "new Array($str)";

?>

0 个答案:

没有答案