嗨,当我尝试运行以下尝试与 PHP 服务器连接以进行身份验证的 jquery 脚本时,我遇到了错误。以下是我的 HTML 脚本:
<head style="background-color:#C6C6FF;">
<meta http-equiv="Content-Type" content="initial-scale=1.0" charset=UTF-8" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/
libs/jquery/1.9.0/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc2/jquery.mobile-1.0rc2.min.css" />
<script type="text/javascript" src="login.js"></script>
</head>
<body>
<div id="login-box" style="background-color:#C6C6FF;" >
<form>
<table border="0" cellpadding="1" cellspacing="7">
<tr>
<td ><div style="font-size:40px; background-color: #C6C6CD;">Login</div></td>
</tr>
<tr>
<td><div style="font-size:20px; color:#0000FF">Username</div><div align="left"><input name="username" id="name" type="text"/></div></td>
</tr>
<tr>
<td><div>Password</div>
<input name="password" id="password" type="password"/></td>
</tr>
<tr>
<td><input type="submit" id="login" value="Submit" onclick="clickPage()"/></td>
</tr>
</table>
</form>
</div>
<div id="outp"></div>
<div height="100px">
用于调用 login.php 文件进行身份验证的 jquery 脚本。
$(document).ready(function(){
var serviceURL = "http://localhost/travel/";
console.log("deviceReady");
});
function clickPage(){
alert('hhhhhhh');
console.log("deviceIN");
var name = $('#name').val();
var password = $('#password').val();
$.ajax({
type: 'POST',
data: {name: name ,
password: password},
url:'http://localhost/travel/login.php',
success: function(data){
//$('#outp').text(status);
alert("Successfully Logged In");
//$('#outp').text(status);
nextPage();
},
error: function(){
//console.log(data,status);
//$('#outp').text(status);
alert("Failure Loggin In");
}
});
return false;
};
function nextPage(){
$.extend( $.mobile , {
ajaxEnabled: false
});
$.mobile.changePage('login.html');
}
这是我的 PHP 脚本
//Include database connection details
require_once('connection.php');
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
//Sanitize the POST values
$username = json_decode($_POST['name']);
$password = json_decode($_POST['password']);
$username = 'admin';
$password = 'admin';
//Input Validations
if($username == '') {
$errmsg_arr[] = 'Username missing';
$errflag = true;
}
if($password == '') {
$errmsg_arr[] = 'Password missing';
$errflag = true;
}
//If there are input validations, redirect back to the login form
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: index.html");
exit();
}
//Create query
$qry="SELECT * FROM login WHERE username='$username' AND password='$password'";
$result=mysql_query($qry);
//Check whether the query was successful or not
if($result) {
if(mysql_num_rows($result) > 0) {
//Login Successful
session_regenerate_id();
$member = mysql_fetch_assoc($result);
$_SESSION['SESS_MEMBER_ID'] = $member['id'];
$_SESSION['SESS_FIRST_NAME'] = $member['username'];
$_SESSION['SESS_LAST_NAME'] = $member['password'];
session_write_close();
echo "Success What";
header("location: mainPage.php");
exit();
}else {
//Login failed
$errmsg_arr[] = 'user name and password not found';
$errflag = true;
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
echo "Failure";
header("location: index.html");
exit();
}
}
}else {
die("Query failed");
}
?>
每次我运行此模块时,我都会收到此错误“登录失败”,这是 ajax 调用失败时的警报。对于此错误,您可以参考上面的 jquery 脚本。我不确定我哪里出错了。