开发者

PHP Ajax is not displaying

I am trying to display from a MySQL Database using PHP and Ajax but the while loop is not displaying my data in the dropdown list do I have a syntax error? here is my code:

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

the php file:

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

$con = mysql_connect('localhost', 'djronlove', 'Djronlove');
if (!$con)
 {
开发者_运维技巧 die('Could not connect: ' . mysql_error());
 }
 mysql_select_db("djronlove", $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);
  ?> 

The url is http://imaginationeverywhere.info/mysql/ajax/ajaxdatabase.html


I didn't see anything obviously wrong. If it was my project, this is what I would do:

1) first see if your php file is even being called by using a proxy. This will help you figure out if the error is from your client code or your php code. (Personally, I use http://www.charlesproxy.com/ --it has a free version so that you can try it out. I bought a license and it has been worth every penny)

2) I would use a javascript library that simplifies the ajax process for cross browser compatibility instead of trying to do ajax with straight up javascript. I recommend jquery http://api.jquery.com/jQuery.get/ This will be especially useful if your problem is in your client code.

3) If your problem is with your PHP code, echo out data to yourself throughout your php file to pinpoint the problem.

4) I would also enable error reporting to yourself in mysql while you are testing like this, so that if your query is causing an error, you will see it:

$result = mysql_query($sql) or die(mysql_error());


If the name of database Table is correct then this while loop should work

while($row = mysql_fetch_array($result), MYSQL_ASSOC){
    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>";
}

Check out this MYSQL_ASSOC, MYSQL_NUM, and MYSQL_BOTH


The problem is in your database query that is not returning any results

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜