1

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

<?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);
});
}

他登录后如何重定向或刷新页面?登录后,索引页面必须刷新..我需要Put window.history.pushState("", "", '/newpage'); 吗?如何使用它?

4

2 回答 2

0
window.top.location.reload();

Use that in your ajax success callback

To redirect instead to a differnt page use:

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

Use:

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();
  });
}

Error handling:

You might want to check for an error, so that if the login is unsuccessful you do not want to refresh the page. To do that relies on knowing what you php function will return E.g.:

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);
        }
      });
    }
于 2013-07-12T08:22:28.843 回答
0

how can i redirect or refresh the Page after he logged in?

by using a regular form submission instead of Ajax.

于 2013-07-12T08:22:43.857 回答