0

我正在查看来自 W3schools.com(Ajax、PHP 和 Mysql) http://www.w3schools.com/php/php_ajax_database.asp的这个脚本

<html>
<head>
<script type="text/javascript">
function showUser(str)
{
if (str=="")
  {
  document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

这显示了一个具有 4 个值的简单选择。

这是 PHP 脚本。

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', '*', '*');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "<td>" . $row['LastName'] . "</td>";
  echo "<td>" . $row['Age'] . "</td>";
  echo "<td>" . $row['Hometown'] . "</td>";
  echo "<td>" . $row['Job'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>

现在我了解了它的作用和工作原理,但是假设我想将另外 3 个变量传递给 PHP 脚本,当有人更改选择框的值时,我该怎么做?

4

3 回答 3

0

为了发送多个值,您已经相应地更改了查询字符串。

xmlhttp.open("GET","getuser.php?q="+str,true);

到,像

xmlhttp.open("GET","getuser.php?q="+str+"&nextvar="+value1,true);
于 2012-04-18T10:20:03.053 回答
0

根据我的评论,这是一个示例,它从 2 个选择中获取值,并在两者都填写时提交一个 ajax 调用

// Notice the arguments are gone at the moment
function showUser() {
    // Retrieve values from the selects
    var u = document.getElementByID('userSelect').value;
    var g = document.getElementByID('groupSelect').value;

    if (u=="" || g == "") {
        document.getElementById("txtHint").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("txtHint").innerHTML=xmlhttp.responseText;
        }
    }

    xmlhttp.open("GET","getuser.php?u="+u+"&g="+g,true);
    xmlhttp.send();
}

然后是基本形式

<form>
    <select name="users" id="userSelect" onchange="showUser()">
        <option value="">Select a person:</option>
        <option value="1">Peter Griffin</option>
        <option value="2">Lois Griffin</option>
        <option value="3">Glenn Quagmire</option>
        <option value="4">Joseph Swanson</option>
    </select>
    <select name="groups" id="groupSelect" onchange="showUser()">
        <option value="">Select a group:</option>
        <option value="a">Aerosmith</option>
        <option value="k">Kiss</option>
        <option value="l">Led Zeppelin</option>
        <option value="m">Metallica</option>
    </select>
</form>

不过,这不是一个很好的选择,并且如前所述,建议您使用诸如 jQuery ( http://jquery.com/ ) 之类的框架,因为您将能够在逻辑上花费更多时间,而不是确保浏览器兼容性

不管你怎么做,尝试一下并没有什么坏处,所以只需尝试一些事情,看看会发生什么(只要你不删除实时数据,无论如何)

于 2012-04-18T10:28:33.483 回答
0

希望这有助于更好

    <script>function showUser(str) {  if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;  } else {    var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    document.getElementById("txtHint").innerHTML = this.responseText;
  }
};
xmlhttp.open("GET","getuser.php?q="+str,true);

xmlhttp.send();  }}function shUser(str1) {  if (str1 == "") { document.getElementById("txtHint").innerHTML = "";
return;  } else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    document.getElementById("txtHint").innerHTML = this.responseText;
  }
};
xmlhttp.open("GET","getuser.php?p="+str1,true); 
xmlhttp.send();  }}function sheUser(str2) {  if (str2 == "") {    document.getElementById("txtHint").innerHTML = "";    return;  } else {   var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {   if (this.readyState == 4 && this.status == 200) {
    document.getElementById("txtHint").innerHTML = this.responseText;
  }
};
xmlhttp.open("GET","getuser.php?r="+str2,true);

xmlhttp.send();  }}
于 2021-11-12T10:00:19.687 回答