登录后刷新页面

时间:2013-07-12 08:20:44

标签: ajax redirect refresh

在索引页面中,用户需要登录.. 登录后,

<?php
    include("dbinit.php");
    $text="";
    $eadd = $_POST['eadd'];
    $pass = $_POST['pass'];         
     if (filter_var($eadd, FILTER_VALIDATE_EMAIL)) {
         $result = mysqli_query($link,"SELECT * FROM account WHERE Eadd='".$eadd."'");
        if (mysqli_num_rows($result)<=0){
          $text = "<font color=red>Invalid Emailaddress and Password Combination!</font>";
        }
        else
        {
         while($row = mysqli_fetch_array($result)){
        $passH = $row['Pass'];
            $passS = $row['hash'];
         }
     if(md5($pass.$passS) == $passH){
            $_SESSION['account'] = $eadd;
            $text = "<font color=red>Login Successful!</font>";
         }else{
            $text = "<font color=red>Invalid Emailaddress and Password Combination!</font>";
         }
            }
        mysqli_free_result($result);
    } else {
      $text = "<font color=red>Invalid Emailaddress!</font>";
    }
                mysqli_close($link);
echo $text;
?>

在索引页面中,

function login(){
var eadd = $('#eadd').val();
var pass = $('#pass').val();
$.ajax({
type: "POST",
url: "login.php",
data: {eadd:eadd, pass:pass}
}).done(function( result ) {
$("#loginMsg").html(result);
});
}

如何在登录后重定向或刷新页面? 登录后,索引页面必须刷新.. 我需要放置window.history.pushState(“”,“”,“/ newpage”);? 怎么用?

2 个答案:

答案 0 :(得分:0)

window.top.location.reload();

在你的ajax成功回调中使用它

要重定向到不同的页面使用:

window.top.location = '/some/page/withcats';

使用:

function login(){
  var eadd = $('#eadd').val();
  var pass = $('#pass').val();
  $.ajax({
    type: "POST",
    url: "login.php",
   data: {eadd:eadd, pass:pass}
  }).done(function( result ) {
    $("#loginMsg").html(result);
    //you may want to check result has no errors or something
    window.top.location.reload();
  });
}

错误处理:

您可能想要检查错误,以便在登录失败时您不想刷新页面。要做到这一点依赖于知道你的php函数将返回例如:

function login(){
      var eadd = $('#eadd').val();
      var pass = $('#pass').val();
      $.ajax({
        type: "POST",
        url: "login.php",
       data: {eadd:eadd, pass:pass}
      }).done(function( result ) {
        //this will make sure the page only refreshes if login is succesful, if not display error
        if(result === "<font color=red>Login Successful!</font>"){
          window.top.location.reload();
        }else{
          $("#loginMsg").html(result);
        }
      });
    }

答案 1 :(得分:0)

  

如何在登录后重定向或刷新页面?

使用常规表单提交而不是Ajax。