-1

我正在尝试使 AJAX 示例正常工作,但无法正常工作。我正在使用 xampp。代码似乎不起作用......顺便说一句,没有目录问题,我确定......我检查了一切......

费率.html

<html>

  <head>
    <script>
      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 & amp; & amp; xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
          }
        }
        xmlhttp.open("GET", "getuser.php?q=" + str, true);
        xmlhttp.send();
      }
    </script>
  </head>

  <body id="top">
    <br />
    <div class="wrapper col3" style="background-image: url(); opacity: 0.7;">
      <?php include( "header.inc"); ?>
    </div>
    <br /><br /><br />
    <div class="wrapper col2">
      <?php include( "menu.inc"); ?>
    </div>
    <br /><br />
    <div class="wrapper col5">
       <h1>Rates</h1>

      <p>Please select an activity to view its rate at Casela. Rates available for both residents and non-residents.</p>
      <form>
        <select name="activity" onchange="showUser(this.value)">
          <option value="">Please select an actvity</option>
          <option value="1">Big Cats (Lions)</option>
          <option value="2">Big Cats (Tigers)</option>
        </select>
      </form>
      <div id="txtHint" style="padding: 20px;"></div>
    </div>
    <br /><br />
  </body>

</html>

获取用户.php

<?php
$q = $_GET["q"];
$con = mysql_connect('localhost', 'root', 'password');
if (!$con) {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("rates", $con);
$sql = "SELECT * FROM activities WHERE id = '" . $q . "'";
$result = mysql_query($sql);
echo "<table border='1'>
    <tr>
    <th>Activity</th>
    <th>Description</th>
    <th>Duration</th>
    <th>Restriction</th>
    <th>Resident</th>
    <th>Non-Resident</th>
    </tr>";
while ($row = mysql_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['Activity'] . "</td>";
  echo "<td>" . $row['Decription'] . "</td>";
  echo "<td>" . $row['Duration'] . "</td>";
  echo "<td>" . $row['Restriction'] . "</td>";
  echo "<td>" . $row['Resident'] . "</td>";
  echo "<td>" . $row['NonResident'] . "</td>";
  echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
4

1 回答 1

5

这一行:

if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)

应该是这样的:

if (xmlhttp.readyState==4 && xmlhttp.status==200)
于 2013-04-18T23:58:35.447 回答