weixin_33670713 2015-03-04 10:56 采纳率: 0%
浏览 29

回声会话数据在ajax?

I am using the following ajax code to login a user by passing the form data entered to my validate_login.php page:

<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>Login In...</h21>'
})  
setTimeout(
  function() 
  {
   window.location = 'dashboard.php'; 
  }, 2500);

  } }
$('#form')[0].reset(); // To reset form fields
});
}
});
});

</script>

I want to point your attention to this part of my code:

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>Login In...</h21>'
    })  

what I'm doing is passing the form data to my php page validate_login.php, this runs a MySQL query to check if the username and password match. If they do then login_success is echoed and this value is passed back to my ajax script as data. This all works fine, the user is logged in and it should echo the users first name to say 'Hello Mark Login In...'

I am trying to do this using a session which is the users first name, and i define this in my validate_login.php page here:

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['id']=$row['user_id'];       //Start User Session
$_SESSION['user']=$username;
$_SESSION['username']=$row['user_first_name'];
$_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";

for some reason, the name will only sometimes be displayed and then other times it won't. I would say it's 50/50.

Is there a reason for this? Please can someone show me what I am doing wrong?

Thanks,

  • 写回答

1条回答 默认 最新

  • 程序go 2017-10-26 15:25
    关注

    you page is already in the client's browser by the time you call ajax to login the user. change your $.post function callback to return user data from server and use jQuery to change your home_column content.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效