php - 堆叠ajax请求

标签 php mysql ajax

感谢您查看此内容。

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

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

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

这两个选项似乎都不起作用。

我想做的事情可行吗?

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

测试.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 :

<?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);
   ?> 

getclub.php

<?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 allclubs WHERE countyid = '".$q."' ORDER by clubname";

$clubresult = mysql_query($sql);




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($clubresult))
  {
  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>";


 mysql_close($cn);
 ?> 

最佳答案

我的建议是按照一些小步骤重新组织代码:

  1. 服务器端代码响应将是json数据
  2. 使用 jquery 进行客户端调用,避免管理低级 HTTP 调用
  3. 使用 jquery 处理程序动态注入(inject)在客户端呈现代码并添加事件处理程序

关于 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."');
});

关于php - 堆叠ajax请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887761/

相关文章:

mysql - Kontakt ibeacon 的 RSSI 统计设置为 Eddystone 格式

mysql - sql消息表查询

javascript - jQuery.ajax 检查是否已经有另一个 xhr

php - 如何从 4 列中有 3 列具有相同值的表中进行选择?

php - 在特定日期执行 SQL 查询

php - 使用MySQL计数函数

php - 使用 php 和 sql 的登录页面无法正常工作

javascript - 第二次按下时链接不会动态加载内容

javascript - Ajax.BeginForm() 在错误时设置 javascript 变量

php - 如何将电子邮件值传递给 url