XMLHTTP:内部服务器错误 - GET 问题?
XMLHTTP: Internal server error - issue with GET?
我正在尝试从存储在服务器上的 MySQL 数据库中检索一些数据。我正在使用 PHP、Javascript 和 AJAX 来获取数据。
当我 运行 Chrome 中的 HTML 文件 (New.html) 并使用开发人员工具查看代码时,它说;
GET http://example.net/Example/getuser.php?q=2 500 (Internal Server Error)
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>
您的代码中有一些拼写错误,如果您修正它们,您的代码将完美运行:
在你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");
现在你可以开始了:)
我正在尝试从存储在服务器上的 MySQL 数据库中检索一些数据。我正在使用 PHP、Javascript 和 AJAX 来获取数据。
当我 运行 Chrome 中的 HTML 文件 (New.html) 并使用开发人员工具查看代码时,它说;
GET http://example.net/Example/getuser.php?q=2 500 (Internal Server Error)
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>
您的代码中有一些拼写错误,如果您修正它们,您的代码将完美运行:
在你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");
现在你可以开始了:)