根据JSON响应重定向用户

时间:2016-01-04 08:00:00

标签: php json ajax

我正在使用bootbox用于UI。除了验证登录凭据并且我需要将用户重定向到login.php之外,我的index.php脚本中的工作正常。
使用PHP,header('Location: index.php');工作正常,但现在因为我使用AJAX使用bootbox,我只希望credentials.php在登录凭据验证后立即重定向到index.php (这次我不想要任何UI互动。)

我的代码在

之下

login.php

<?php

require_once 'connect.php';
session_start();

if($_SERVER['REQUEST_METHOD'] === 'POST'){


    if(
       isset($_POST['email']) &&
       isset($_POST['password']) &&
       !empty($_POST['email']) &&
       !empty($_POST['password']))
    {

            // Get post data

                $email = htmlentities(mysqli_real_escape_string($connection, trim($_POST['email'])));
                $password = sha1(md5(htmlentities(mysqli_real_escape_string($connection, trim($_POST['password'])))));
                $status = 1; // Here we set by default status In-active.

                if(filter_var($email, FILTER_VALIDATE_EMAIL)) {

                $email_query = "SELECT * FROM users WHERE email = '{$email}'";
                $check_email = mysqli_query($connection, $email_query);

                if($check_email) {

                    $email_count = mysqli_num_rows($check_email);

                    if($email_count == 1) {

                        $email_row = $check_email->fetch_assoc();
                        $email_address = $email_row['email'];

                        $password_query = "SELECT password FROM `users` WHERE email = '{$email_address}'";
                        $check_password_query = mysqli_query($connection, $password_query);

                        $password_row = $check_password_query->fetch_assoc();
                        $password_from_database = $password_row['password'];

                        if($password === $password_from_database) {

                            $_SESSION['email'] = $email;
                            header('Location: index.php');


                        }
                        else {
                            $data = array("result" => -7, "message" => "Wrong Password!");
                        }


                    }
                    else {
                            $data = array("result" => -6, "message" => "Email Address Is Not Registered!");
                    }
                }
                else {
                    $data = array("result" => -5, "message" => "Email Address Could Not Be Validated! Try again later.");
                }

            }
            else {
                $data = array("result" => -4, "message" => "Invalid Email Address! yourname@domain.com is an example
                              of the correct format.");
            }
    }
    else {
            $data = array("result" => -3, "message" => "All Fields Are Mandatory!");
        }
    }
else {
    $data = array("result" => -1, "message" => "Incorrect Request Method!");
}




mysqli_close($connection);
/* JSON Response */
header('Content-type: application/json');
echo json_encode($data, JSON_PRETTY_PRINT);

&GT;

credentials.php

$(document).ready(function() {

// process the form
$("#login-form").submit(function(event) {

    // get the form data
    // there are many ways to get this data using jQuery (you can use the class or id also)
    var formData = $("#login-form").serialize();


    // process the form
    $.ajax({
        type        : 'POST', // define the type of HTTP verb we want to use (POST for our form)
        url         : 'login.php', // the url where we want to POST
        data        : formData, // our data object
        dataType    : 'json', // what type of data do we expect back from the server
        encode      : true
    })
        // using the done promise callback
        .done(function(data) {
        bootbox.dialog({
        message: data.message,
        title: "What's Up ?",
        buttons: {
            success: {
                label: "Okay",
                className: "btn-success", callback: function() {
                  if (data.message === "Email Address Is Not Registered!") {

                    $("#email").val('');

                  }else if (data.message === $("#firstname").val()+" "+ $("#lastname").val()+" has been successfully registered and a verification email has been sent to "+$("#email").val()+". Don't forget to check your JUNK or SPAM folder!") {

                    var email = $("#email").val();
                    var domain = email.replace(/.*@/, "");
                    var address = "http://"+domain;
                    bootbox.confirm("Do you want to go to " + domain , function(result){

                      if (result === true) {

                            $("#register-form")[0].reset();
                            var redirectWindow = window.open(address, '_blank');
                            redirectWindow.location;

                        } else {
                            $("#register-form")[0].reset();
                            console.log("User declined dialog");
                        }

                    })


                  }


                }
            },
        }
    });
            // log data to the console so we can see
            console.log(data);



            // here we will handle errors and validation messages
        });

    // stop the form from submitting the normal way and refreshing the page
    event.preventDefault();
});

});

0 个答案:

没有答案