重定向不会分开页面

时间:2015-02-16 17:50:16

标签: php ajax

我有一个登录页面,使用Ajax将用户名和密码发送到php文件进行验证。如果验证失败,它会在创建帐户按钮下的登录页面上向用户发送一条消息,说明此情况。这很好。如果用户/密码正确,我想重定向到用户帐户页面。这也是有效的,但它最终与验证失败的位置相同 - 在创建帐户按钮下的登录页面上。我理解为什么会发生这种情况,我不知道如何让重定向只是在它自己的页面上显示新页面。感谢

亲眼看看 - kyzereyephotography.com/examples/signIn/signIn.php

用户名/通行证 - 123@123.com / 123

使用不正确的密码查看失败的消息

以下是代码:

signIn.php

<!DOCTYPE html>
<html>
  <head>
    <title>Sign IN</title>
    <link href="../dist/css/bootstrap.css" rel="stylesheet">
    <link href="../dist/css/signin.css" rel="stylesheet">
    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
  </head>
  <body>
    <!-- Fixed navbar -->
    <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <a class="navbar-brand" href="../">Code Example - Log In</a>
        </div>
      </div>
    </div> </br></br>

<!-- Sign In Form -->
    <div class="container">
      <form class="form-signin" id="login_form" action="checkLogin.php" method="post">
        <p>
          This bit of code asks you to log in.  If you do not have an account, you can create one and then log on to it. 
        </p>
        <h2 class="form-signin-heading">Please sign in</h2>
        <input name="username" id="username" type="text" class="form-control" placeholder="Email address" required autofocus>
        <input name="password" id="password" type="password" class="form-control" placeholder="Password" required>
        <button id="signin" class="btn btn-lg btn-primary btn-block" type="submit" >Sign in</button>
      </form>
<!-- Create Accout Form -->
      <form class="form-signin">
         <input class="btn btn-lg btn-primary btn-block" type="button" value = "Create Account" onclick="window.location.href='createAccount.php'"/>
      </form>
        <span id="badpass"></span> <!--place to put the error message if password or username is bad-->
    </div> <!-- /container -->

    <!-- JavaScript-->
  <!-- this script is used to validate the username and password.  JavaScript is used to pass data to checklogin.php-->
    <script type="text/javascript">
      $("#signin").click( function() {
      var data = $("#login_form :input").serializeArray();
      $.post( $("#login_form").attr("action"), data, function(info) { $("#badpass").html(info);});
      });
      $("#login_form").submit(function() {
        return false;
      });
    </script>
 </body>
</html>

checkLogin.php

<?php
session_start();
    include "includes/dbconnect.php";

    $username = $_POST['username'];
    $userPassword = $_POST['password'];


    $query1 = "SELECT first_name, last_name, password FROM profiles WHERE email = '$username'";

    $results1 = mysql_query($query1,$dbconnect) or die ("Error in query1. ". mysql_error($dbconnect));      
    $row = mysql_fetch_assoc($results1);
    $count = mysql_num_rows($results1);
    $hashedPass = $row['password'];

    if($count == 1)
    {
        if (crypt($userPassword, $hashedPass) == $hashedPass)
        {
            $_SESSION["username"] = $username;

            header("location: profilesPage.php");
            die();

        }
        else
        {
            echo "<div style='color: red; font-size: 20px; text-align: center;'>ID or Password does not match</div>";
        }
        exit();

    }
    else
    {
        echo "<div style='color: red; font-size: 20px; text-align: center;'>Bad username or password</div>";
    }
?>

1 个答案:

答案 0 :(得分:0)

首先:出于安全原因,请将您的脚本更新为MySQLi()PDO()!不推荐使用Mysql!

接下来,我对你的问题的方法是这样的:

$("#signin").click( function() {
    var data = $("#login_form :input").serializeArray();
    $.post(
        $("#login_form").attr("action"),
        data,
        function(info) {
            if(info == ""){ //Insert good password response here
                window.location.href = ""; //Url to profile page here
            } else {
                $("#badpass").html(info);
            }
        }
    );
});