AJAX无法在localhost上工作?

时间:2013-04-18 23:49:58

标签: php javascript ajax xampp

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

rates.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>

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

1 个答案:

答案 0 :(得分:5)

这一行:

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

应该像:

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