我想轉換我的登錄頁面使用HTML和使用jQuery和PHP來獲取和處理結果,原因是如果我想移動與我的項目,那麼我幾乎在那裏。將變量從php傳遞迴jquery?
我遇到的問題是將變量從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
}
的login.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();
當登錄失敗有我想要的各種原因,你可以看到返回到警報框。什麼是最好的方式去做這個
我花了正好2分鐘格式化你的答案(用奇怪的fromatting)......該死的!!我仍然看到一些額外的空間.. :( – bipen 2013-03-25 11:43:22
你沒有提交表格任何地方。 – Jai 2013-03-25 11:45:15
對不起Bipen下次會確定它的整潔 – 2013-03-25 11:58:43