Php 堆叠ajax请求

Php 堆叠ajax请求,php,mysql,ajax,Php,Mysql,Ajax,谢谢你检查这个 我在mysql中有一个数据库,它当前填充了一个下拉菜单,在select上使用ajaxxmlhttprequest将第二个下拉菜单填充到运行mysql查询的php文件 然后,我想根据第二个下拉列表中的选择显示一个表,同时仍保留第一个下拉列表的功能 到目前为止,我已经尝试向主文档添加第二个js调用,并且还尝试将该js调用放入第一个调用的php文件的输出中 这两种选择似乎都不起作用 我想做的事可能吗 日志文件从未显示它试图获取getclubs.php,因此我假设GetClub调用从未被

谢谢你检查这个

我在mysql中有一个数据库,它当前填充了一个下拉菜单,在select上使用ajax
xmlhttprequest将第二个下拉菜单填充到运行mysql查询的php文件

然后,我想根据第二个下拉列表中的选择显示一个表,同时仍保留第一个下拉列表的功能

到目前为止,我已经尝试向主文档添加第二个js调用,并且还尝试将该js调用放入第一个调用的php文件的输出中

这两种选择似乎都不起作用

我想做的事可能吗

日志文件从未显示它试图获取getclubs.php,因此我假设GetClub调用从未被触发

Test.php:

<html>
<title>
demo </title>
<head>
<script>
function GetCounty(str)
 {
 if (str=="")
   {
   document.getElementById("countymenu").innerHTML="";
    return;
    } 
 if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
  }
      else
      {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
   {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("countymenu").innerHTML=xmlhttp.responseText;
     }
}
xmlhttp.open("GET","getcounty.php?q="+str,true);
xmlhttp.send();
}
</script>
<script>
 function GetClubs(str)
 {
 if (str=="")
   {
   document.getElementById("clubtable").innerHTML="";
   return;
   } 
if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
  else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }
  xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
 {
 document.getElementById("clubtable").innerHTML=xmlhttp.responseText;
 }
   }
 xmlhttp.open("GET","getclub.php?q="+str,true);
 xmlhttp.send();
  }
  </script>
  </head>
  <body>


<?
 // Load field datas into List box
 $cn=mysql_connect("localhost","user","password") or die("Note: " . mysql_error());
 echo "Conn ok<br>";
 $res=mysql_select_db("snowusa_clubs",$cn) or die("Note: " . mysql_error());
 echo " Database opened<br>";
 //$rescounty=mysql_query("SELECT * FROM county WHERE state_id='33' ORDER by name;") or die ("Note: " . mysql_error());
    $resstate=mysql_query("SELECT * FROM state ORDER by longstate;") or die("Note: " . mysql_error());
echo " qry executed<br>";
 ?>
 <h1>Select</h1>


State:
 <select name="State" size=1 onchange="GetCounty(this.value)">
 <option value="">Select a State</option>
 <?
  while($rs = mysql_fetch_array($resstate))
 {
 echo "<option value=" .$rs['id'] . ">" . $rs['longstate'] . "</option>";
 }
 echo "</select> "
 ?>
 <p>
</p>
<div id="countymenu"><b>County menu for selected state will be listed here.</b></div>




 </body>
 </html>
<?php
$q=$_GET["q"];

$cn=mysql_connect("localhost","user","password");
if (!$cn)
   {
   die('Could not connect: ' . mysql_error());
    }

 mysql_select_db("snowusa_clubs", $cn);

 $sql="SELECT * FROM county WHERE state_id = '".$q."' ORDER by name";

 $result = mysql_query($sql);

 $fulllist="SELECT * FROM allclubs WHERE stateid = '".$q."' ORDER by clubname";
 $listresult = mysql_query($fulllist);




 echo "County : <select name=\"County\" size=1 onchange=\"GetClub(this.value)\">";

 echo "<option value=\"\">Select County</option>";

 while($rc = mysql_fetch_array($result))
 {
 echo "<option value=" .$rc['id'] . ">" . $rc['name'] . "</option>";
 }
 echo "</select>";

 echo "<p></p>";
 echo "Table of All Clubs in Selected State:</br>";

 echo "<table border='1'>
 <tr>
 <th>County</th>
 <th>Club Name</th>
 <th>Address</th>
 <th>Phone</th>
 <th>Website</th>
 <th>Email</th>

 </tr>";

 while($row = mysql_fetch_array($listresult))
   {
   echo "<tr>";
   echo "<td>" . $row['county'] . "</td>";
   echo "<td>" . $row['clubname'] . "</td>";
   echo "<td>" . $row['address'] . "</td>";
   echo "<td>" . $row['phone'] . "</td>";
   echo "<td>" . $row['website'] . "</td>";
   echo "<td>" . $row['email'] . "</td>";
   echo "</tr>";
     }
   echo "</table>";

   echo "<div id='clubtable'><b>Club Listing will appear as a table here.</b></div>";

   mysql_close($cn);
   ?> 

演示
功能县(str)
{
如果(str==“”)
{
document.getElementById(“countymenu”).innerHTML=“”;
返回;
} 
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“countymenu”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“getcounty.php?q=“+str,true”);
xmlhttp.send();
}
功能俱乐部(str)
{
如果(str==“”)
{
document.getElementById(“clubtable”).innerHTML=“”;
返回;
} 
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“clubtable”).innerHTML=xmlhttp.responseText;
}
}
open(“GET”、“getclub.php?q=“+str,true”);
xmlhttp.send();
}

我的建议是按照以下几个小步骤重新组织代码:

  • 服务器端代码响应将是json数据
  • 使用jquery进行客户端调用,避免管理低级HTTP调用
  • 动态使用jquery处理程序注入在客户机上呈现代码并添加事件处理程序
  • 关于jquery的一些参考资料:

    - GET HTTP calls with : http://api.jquery.com/jQuery.get/
    - how to add a event : http://api.jquery.com/bind/
    
    一些例子:

    对test.php的简单GET HTTP调用

    $.get("test.php", function(data) {
      alert("Data Loaded: " + data);
    });
    
    向id=foo的元素添加单击

    $('#foo').bind('click', function() {
      alert('User clicked on "foo."');
    });
    

    onchange=“GetClub…”不带“s”。您的实际函数名有's',GetClubs.it:):smackhead:请注意,
    XMLHttpRequest
    在IE7+中要到10才能正常工作。相反,你应该停止重新发明轮子,开始使用流行的javascript库,比如
    Jquery
    I don't knowledge。为什么人们不使用好的IDE?它与IDE无关。没有什么能阻止人们用最好的IDE编写蹩脚的代码。当他可以使用
    .on()
    时,他为什么要使用
    .bind()
    ?从jQuery 1.7开始,
    .on()
    方法是将事件处理程序附加到文档的首选方法。感谢这些指针,我必须查看这些引用,看看它们是否有意义:)@RPM
    $(..)。on(..)
    仅当我们期望从服务器响应中获得新的
    DOM
    元素时才应使用。常用的方法是
    $(“选择器”)。单击(function(){…})
    $('#foo').bind('click', function() {
      alert('User clicked on "foo."');
    });