dongwopu8210 2015-12-08 14:40
浏览 48

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

I'm trying to update drop down list with the other drop down list using Ajax. Somehow it is not working. I'm not sure which path is going wrong.

php file

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

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 yolov8边框坐标
    • ¥15 matlab中使用gurobi时报错
    • ¥15 WPF 大屏看板表格背景图片设置
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真