XMLHTTP:内部服务器错误 - GET问题?

时间:2016-11-19 22:49:18

标签: javascript php mysql ajax internal-server-error

我正在尝试从存储在服务器上的MySQL数据库中检索一些数据。我正在使用PHP,Javascript和AJAX来获取数据。

当我在Chrome中运行HTML文件(New.html)并使用开发人员工具查看代码时,它说;

  

获取http://example.net/Example/getuser.php?q=2 500(内部服务器错误)

     

showUser @ New.html:31onchange @ New.html:52

我认为这是指xmlhttp.onreadystatechange,并且.send()行旁边有一个红色的X.

<html>
<head>
    <title>New</title>
    <meta charset="UTF-8">
                              //Javascript Code
<script> 
        function showUser(str) {
            if (str == " ") {
                document.getElementById("txtHINT").innerHTML = " ";
                return;
            } else {
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firfox, Chrome, Opera, Safari
                    xmlhttp = new XMLHttpRequest();  
            } else {
                   // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    document.getElementById("txtHint").innerHTML = this.reponseText;
                }
            };
            xmlhttp.open("GET","getuser.php?q="+str,true);
            xmlhttp.send(); //LINE 31
        }
    }
</script>
                      //CSS for HTML table
<style> 
    table         {
        width: 100%;
        border-collapse: collapse;
    }
    table, td, th {
        border: 1px solid black;
        padding: 5px;
    }   
    th            {
       text-align: left;
    }
</style>
</head>
                   <!-- Code for Form -->
<body>
    <form>
        <select name ="users" onchange="showUser(this.value)"> //LINE 51
            <option value=" ">Select a person:</option>
            <option value="1">Peter Griffin</option>
            <option value="2">Lois Griffin</option>
            <option value="3">Joseph Swanson</option>
            <option value="4">Glenn Quagmire</option>
        </select>
    </form>
    <br>
    <div id="txtHint"><b>MySQL Data should go here</b></div>
</body>
</html>

有谁知道如何解决这些问题?也许.open()需要在代码中提前放置?或者可能需要某种处理程序?

PHP文件:

<html>
<head>
    <title>Latest Attempt</title>
</head>
<?php
    $q = intval($_get['q']);
    // put your connection code here
    $servername = 'localhost';
    $username = 'user';
    $password = '12345678';
    $dbname = 'ajax_demo';

    // create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    echo "Connected successfully";

    mysqli_select($conn,"ajax_demo");
    $sql="SELECT * FROM my_DB WHERE id = '".$q."'";
    $result = mysqli_query($conn,$sql);

    echo "<table>
    <tr>
    <th>FirstName</th>
    <th>LastName</th>
    <th>Age</th>
    <th>Hometown</th>
    <th>Job</th>
    </tr>";
    while ($row = mysqli_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>";
    mysqli_close($conn);
?>
</html>

1 个答案:

答案 0 :(得分:1)

您的代码中有一些拼写错误,如果您修复它们,您的代码将完美运行:

在JavaScript中

在这一行

document.getElementById("txtHint").innerHTML = this.reponseText;

你拼错了reponseText,它应该是responseText所以这一行就像是:

document.getElementById("txtHint").innerHTML = this.responseText;

在您的PHP中

您必须从页面顶部删除所有这些额外的html标记:

<html>
<head>
    <title>Latest Attempt</title>
</head>

和页面底部的标签:

</html>

然后你就在这一行:

$q = intval($_get['q']);

你必须在资本中输入$ _GET,这样就像:

$q = intval($_GET['q']);

最后mysqli没有mysqli_select()函数,所以你必须完全删除这一行:

mysqli_select($conn,"ajax_demo");

现在你很高兴:)