成功的ajax请求完成后未重定向到另一个页面

时间:2018-11-27 09:07:06

标签: php ajax

我正在通过ajax验证登录表单。成功验证后,表单不会重定向到所需页面。

Ajax代码

function login_submit(){
    var stat="";
    $("#submit").val("Loging in...");
        $.ajax({
              type: "POST",
              url: "php/login.php",
              data: {
                      uname: $("#uname").val(),
                      pass : $("#pass").val()
                    },
              success: function(result) {
                          if(result=="parent"){
                              window.location = "http://localhost:90/auction/augeo/admin/parent_admin/index";
                          }
                          else if(result == "sucess_normal"){
                              window.location.assign("../normal_admin");
                            }
                          else if(result == "deactivated account") {
                              window.location.assign("reactivate_account/");
                            }
                          else if(result == "banned account") {
                              window.location.assign("banned_account/");
                          }
                          else{

                            $("#submit").val("Login");
                            $("#error_msg").css({color: 'red'});
                             document.getElementById("error_msg").innerHTML= result;
                             stat = false;


                      }
                    }
               });
    if(!stat)
      return false;
  }

PHP代码

if(isset($_POST['uname']) && isset($_POST['pass'])){
    $username = encode($_POST['uname']);
    $password = encrypt(encode($_POST['pass']));

    // check if entered username and password is in the database
    $result = mysqli_query($conn,"SELECT * From admin_account where admin_account.username = '$username' AND admin_account.password = '$password' ");
    if($row = mysqli_num_rows($result) == 1){
        $found = mysqli_fetch_array($result);

        if($found['state'] == 1){
            $account_id = $found['account_id'];
            setcookie("admin_id", $account_id, time() + (86400 * 30), "/");
            $_SESSION['admin_id'] = $account_id;


            $result1 = mysqli_query($conn,"SELECT role_id From admin where admin_id = '$account_id'");
            $found1 = mysqli_fetch_array($result1);
            $_SESSION['account_type'] = $found1['role_id'];
            if($found1['role_id'] == "1"){
                echo "parent";
           //header("Location: http://localhost:90/auction/augeo/admin/parent_admin/index");
            }else{
                echo "sucess_normal";
            }
        }
        elseif($found['state'] == 2){
            echo "banned account";
        }
        else{
            $_SESSION['deactivated_id'] = $found['account_id'];
            echo "deactivated account";
        }
    }
    else{
        echo "Incorrect Username or Password";
    }
}

我尽了一切努力,但无济于事。我想检查 result ==“ parent” ,如果 result ==“ parent” 应该重定向到 window.location =“ http://localhost:90/auction/augeo/admin/parent_admin/index “; ,但它会回显父级

2 个答案:

答案 0 :(得分:0)

您说“它正在回荡父母”。 但是您提供的AJAX代码永远都不会发生这种情况

所以我怀疑您有一个正在运行其自身默认提交的表单,而是您所看到的。

您可能想签出this answer

$('#idOfYourForm').submit(function() {
     var $theForm = $(this);

     // This is a button or field, right? NOT the form.
     $("#submit").val("Logging in...");

     $.post(
          'php/login.php',
          {
                  uname: $("#uname").val(),
                  pass : $("#pass").val()
          }
     ).done(function(result) {
          // check the result
          alert("Server said: " + result);
     });

     // prevent submitting again
     return false;
});

答案 1 :(得分:0)

您将获得带有按钮

$("#submit")

可以,但是如果按钮定义为:

<input type="submit" id="submit" value="..." />

您将随后提交定义按钮的表单。

为避免这种情况,对另一个建议的解决方案要简单得多,那就是根本不使用提交按钮。而是使用简单的操作按钮。这是两个示例,第二个示例可能更好,因为使用bootstrap / HTML5 / CSS进行设计更容易...

<input type="button" id="submit" value="..." />

或更好:

<button type="button" id="submit">...</button>

在服务器/网络速度慢的情况下,您可能希望通过禁用按钮来提高AJAX的可用性:

$("#submit").val("Logging in...").prop("disable", "disable");

这有助于避免服务器运行缓慢且用户不耐烦时提交多个文件。