我尝试通过Ajax将HTML表单中的数据发送到php代码并且它没有得到响应
html表单从用户条目获取user_id然后通过处理ajax代码的java脚本函数发送它并将user_id发送到php代码以通过$user_id = $_GET['user_id'];
获取user_id并通过user_id搜索然后显示php中的内容其他html代码中的代码显示div内容显示来自php的document.getElementById("content").innerHTML=xmlhttp.responseText;
响应
function showUser(str) {
if (str == "") {
document.getElementById("content").innerHTML = "";
return;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("content").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("POST", "http://localhost/gpms/admin_modify_user.php?user_id=" + str, true);
xmlhttp.send();
}

<head>
<script src="http://localhost/gpms/admin_user.js"></script>
</head>
<body>
<form method=post>
<label> Enter User ID: </label>
<input id="user_id" type=text name=user_id>
<br><br>
<input id="modify" type=submit value=Modify onclick="<script>showUser(user_id);</script>">
</form>
</body>
&#13;
<?php
$user_id = $_GET['user_id'];
//create connection
$conn = mysqli_connect("localhost","root","","gpms");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM user WHERE user_id = '$user_id' ";
$result = mysqli_query($conn, $sql);
if (mysqli_query($conn, $sql)) {
$row = mysqli_fetch_assoc($result);
if ($row == 0) {
echo "No Results";
}
else {
$id = $row['user_id'];
$name = $row['user_name'];
$password = $row['user_password'];
$email = $row['user_email'];
$department = $row['user_department'];
echo "<div id = demo>";
echo "<table>";
echo "<tr>";
echo "<td> ID </td><td> Name </td><td> Password </td><td> E-mail </td><td> Department </td>";
echo "</tr>";
echo "<tr>";
echo '<td> '. $id .' </td><td> '. $name .' </td><td> '. $password .' </td><td> '. $email .' </td><td> '. $department .' </td>';
echo "</tr>";
echo "</table>";
echo "<button onclick = 'editUser(\"$id\",\"$name\",\"$password\",\"$email\",\"$department\")' > Edit </button>";
echo "<button onclick = 'deleteUser(".$id.")' > Delete </button>";
echo "</div>";
}
}
else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$conn->close();
?>
&#13;
答案 0 :(得分:0)
您的onclick
属性错误。它应该只是Javascript代码,它不应该有<script>...</script>
。
<input id="modify" type=submit value=Modify onclick="showUser(user_id);">
如果你查看过你的Javascript控制台,你会看到它在你点击时抱怨语法错误。
在PHP中,这一行:
if (mysqli_query($conn, $sql)) {
应该是:
if ($result) {
否则,您执行两次相同的查询,这是不必要的。