我使用以下ajax脚本将用户登录到我的网站。提交时的ajax脚本将表单数据传递给我的页面validate_login.php:
的index.php:
<script src="assets/jquery/jquery.min.js"></script>
<script type="text/javascript" src="assets/jquery/flip/jquery.flip.js"></script>
<script type="text/javascript" src="assets/jquery/flip/jquery.flip.min.js"></script>
<script src="assets/jquery/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(document).on('click', '#submit', function() {
var myusername = $("#myusername").val();
var mypassword = $("#mypassword").val();
if (myusername == null || myusername == "" || mypassword == null || mypassword == "") {
if (myusername == null || myusername == "") { document.forms["form"]["myusername"].style.border = "2px solid #963634";}
if (mypassword == null || mypassword == "") { document.forms["form"]["mypassword"].style.border = "2px solid #963634";}
$( ".home_column" ).effect( "shake" );
} else {
// Returns successful data submission message when the entered information is stored in database.
$.post("include/validate_login.php", {
username1: myusername,
password1: mypassword
}, function(data) {
if(data == 'login_wrong') {
$(".home_column").flip({
direction:'lr',
color: 'rgba(138, 138, 138, 0.2)',
content:'<h21>Incorrect Login Details</h21>'
})
setTimeout(
function()
{
$(".home_column").revertFlip()
}, 2500); }else{
if(data == 'login_success') {
$(".home_column").flip({
direction:'lr',
color: 'rgba(138, 138, 138, 0.2)',
content:'<h21>Hello <? echo $_SESSION['username']; ?></h21><br/><br/><h21>Please Wait...</h21>'
})
setTimeout(
function()
{
window.location = 'dashboard.php';
}, 2500);
} }
$('#form')[0].reset(); // To reset form fields
});
}
});
});
</script>
validate_login.php,在此页面中我运行MySQL查询以检查用户名和密码是否匹配。成功查询后,我回复了“login_success”#39;或者一旦失败,我就会回复#login_wrong&#39;。
validate_login.php:
session_start();
$query = "SELECT * FROM $tbl_name WHERE user_name = '$username' UNION
SELECT * FROM $tbl_name2 where user_name = '$username'";
$result = mysql_query($query) or die( mysql_error() );
$row = mysql_fetch_assoc($result);
$hashed_pass = crypt($password, $Blowfish_Pre . $row['salt'] . $Blowfish_End);
if(mysql_num_rows($result) > 0) { // Check Username Exists
if($hashed_pass === $row['user_password_hash']) { // Check Password is valid
$_SESSION['username']=$row['user_first_name']; //Start User Session
$_SESSION['id']=$row['user_id'];
$_SESSION['user']=$username;
$_SESSION['username2']=$row['user_last_name'];
$_SESSION['allowance']=$row['allowance'];
$_SESSION['privelleges']=$row['privelleges'];
$sql2 = mysql_query("UPDATE $tbl_name2 SET user_failed_logins = '0', user_status = 'online' WHERE user_name = '".$username."'");
$result2=mysql_query($sql2);
$sql3 = mysql_query("UPDATE $tbl_name SET user_failed_logins = '0', user_status = 'online' WHERE user_name = '".$username."'");
$result3=mysql_query($sql2); //Start User Session
echo "login_success";
}else{
echo "login_wrong";
然后我使用我的ajax代码收集这些回显的值作为数据,我在我的ajax中执行检查以查看数据是否等于&#39; login_success&#39;或者login_wrong&#39;
if(data == 'login_success') {
如果登录成功,我会执行一个效果,使我的div翻转并显示欢迎信息:
if(data == 'login_success') {
$(".home_column").flip({
direction:'lr',
color: 'rgba(138, 138, 138, 0.2)',
content:'<h21>Hello <? echo $_SESSION['username']; ?></h21><br/><br/><h21>Please Wait...</h21>'
})
我放<?php echo $_SESSION['username']; ?>
这是假设显示我们之前在validate_login.php中设置此变量的用户名。但是,在会话有时间设置/启动之前,ajax似乎正在运行。这意味着会话为空,而不是Hello Michael
,我得到Hello
。
所以我试图在我的validate_login.php中回复<?php echo $_SESSION['username']; ?>
和我的echo "login_success";
而不是像我这样的ajax:
echo "login_success";
echo $_SESSION['username'];
}else{
echo "login_wrong";
理论上,数据(即用户的名字)应该与其他数据一起收集&#39; login_success&#39;。
但是,我可以在ajax中收集多个数据吗?因为如果我这样做,我就不能再执行if语句来查看数据=&#39; login_success&#39;它只是阻止整个脚本完全工作,提前谢谢
答案 0 :(得分:0)
您可以通过以下方式将数据作为JSON发回:
echo json_encode( array(
"item1" => $value1,
"item2" => $value2
));
然后在JS:
$.post("include/validate_login.php", {
username1: myusername,
password1: mypassword
}, function(json) {
console.log(json.item1);
console.log(json.item2);
}, 'json'); // Don't forget this