我使用post方法通过XMLHttpRequest对象将数据(用户名和密码)从xdk app发送到php文件,但我遇到了问题。 我通过post文件发送的所有数据都是在php文件中未定义的。
这个xdk代码
<!DOCTYPE html>
<html lang="en-US">
<head>
<script type="text/javascript">
function login() {
var xhr = new XMLHttpRequest();
var username1 = document.getElementById("user_name").value;
var pass = document.getElementById("pass_word").value;
var params = "username=" + username1 + "&password=" + pass;
alert(params);
xhr.open("post", "http://localhost/hospital/test.php", false);
xhr.onload = function () {
if (xhr.status == 200) {
var json_string = xhr.responseText;
test.innerHTML = json_string;
}
else if (xhr.status == 404) {
alert("Web Service Doesn't Exist", "Error");
}
else {
alert("Unknown error occured while connecting to server", "Error");
}
}
xhr.send(params);
}
</script>
</head>
<body>
<div class="container">
<div id="login">
<img src="img/logo.png" width="100 px" height="100 px"/>
<form onsubmit="login(); return false;">
<fieldset class="clearfix">
<p><span class="fontawesome-user"></span><input type="text" id="user_name"/></p>
<p><span class="fontawesome-lock"></span><input type="password" id="pass_word"/></p>
<p><input type="submit" value="تسجيل الدخول"/></p>
</fieldset>
</form>
<div id="test"></div>
</body>
</html>
这是php代码
<?php
$con = mysqli_connect("localhost", "root", "", "interactive");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST["username"])) {
$username = $_POST["username"];
$password = $_POST["password"];
}
$sql = "SELECT * FROM Caregiver WHERE UserName = $username && password = $password ";
$result = mysqli_query($con, $sql);
$num = $result->num_rows;
if ($num) {
$row = $result->fetch_array(MYSQLI_BOTH);
$data = array('status' => 'success', 'username' => $row['UserName'], 'password' => $row['password']);
} else {
$data = array('status' => 'failure', 'Error: ' . mysqli_error($con));
}
echo json_encode($data);
mysqli_close($con);
?>
答案 0 :(得分:0)
这在我的驾驶室外面有一点点,但我想你可能需要一个端口:
xhr.open("post", "http://localhost/hospital/test.php", false);
像
xhr.open("post", "http://localhost:port/hospital/test.php", false);