我用post方法通過XMLHttpRequest對象從xdk應用程序發送數據(用戶名和密碼)到php文件,但是我遇到了一個問題。 我通過post方法發送的所有數據在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);
?>
'WHERE UserName = $ username && password = $ password'那些是字符串值,不是整數。 –
是的,這些都是字符串數據,但是當我使用GET方法時,在命令中沒有錯誤。 – Malak
沒有人幫助我:( – Malak