我正在尝试将我的登录页面转换为使用 html 并使用 jquery 和 php 来获取和处理结果,原因是如果我想与我的项目一起使用移动设备,那么我就快到了。
我遇到的问题是将变量从 php 传递回 jquery 以同时显示。
我的示例 index.html
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Auth Demo 2</title>
<script src="jquery/jquery-1.7.2.min.js"></script>
<script src="main.js"></script>
</head>
<body onload="handleLogin()">
<form id="loginForm">
<label for="email">Email:</label>
<input type="text" name="email" id="email" value="" placeholder="email" />
<label for="password">Password:</label>
<input type="password" name="password" id="password" value="" placeholder="Password" />
<input type="submit" value="Login" id="submitButton">
</form>
</body>
</html>
main.js
function handleLogin(){
var form = $("#loginForm");
var u = $("#email", form).val();
var p = $("#password", form).val();
if(u!= '' && p!= '') {
$.post("http://www.mysite.co.uk/login.php",{
email:$('#email', form).val(),
password:$('#password', form).val(),
rand:Math.random()
} ,function(data)
{
if(data=='yes') //if correct login detail
{
alert( "Request success: ");
}
else
{
//add reason in alert box here
alert ("failed reason")
}
});
} else {
alert( "Username/password empty");
}
return false;//not to post the form physically
}
登录.php
<?//get the posted values
require_once("../backend/functions.php");
dbconn(true);
if ($_POST["email"] && $_POST["password"]) {
$password = passhash($_POST["password"]);
if (!empty($_POST["email"]) && !empty($_POST["password"])) {
$res = SQL_Query_exec("SELECT id, password, secret, status, enabled FROM users WHERE email = " . sqlesc($_POST["email"]) . "");
$row = mysql_fetch_assoc($res);
if ( ! $row || $row["password"] != $password )
$message = "Wrong password";
elseif ($row["status"] == "pending")
$message = "Account Pending";
elseif ($row["enabled"] == "no")
$message = "Account Suspened";
} else
$message = "No Username/password added";
if (!$message){
logincookie($row["id"], $row["password"], $row["secret"]);
if (!empty($_POST["returnto"])) {
header("Refresh: 0; url=" . $_POST["returnto"]);
die();
}
else {
echo "yes";
die();
}
}else{
echo $message;
}
}
logoutcookie();
正如您所看到的,登录失败时有各种原因我想传回警报框。解决这个问题的最好方法是什么