Carpetfizz
Carpetfizz

Reputation: 9149

"Continue" a PHP redirect with AJAX?

I have an HTML form that is processed via PHP, and it's a simple login script. This works fine, but I want to catch the error with AJAX. Basically, if the PHP function to check the login returns false, it should write that error onto the DOM via jQuery. However, if I catch the form submission with AJAX, it will stop the PHP file from doing the redirect. I want to stop the form from redirecting only if the PHP file returns false. Any help would be much appreciated. Here's some code to illustrate what I'm talking about:

controller_login.js

$(document).ready(function(){

$('form.loginSubmit').on('submit',function(){


    var that = $(this),
        url=that.attr('action'),
        type=that.attr('method'),
        data={};

    that.find('[name]').each(function(index,value){ 
        var that=$(this), 
            name=that.attr('name'); 
            value=that.val(); 
            data[name]=value;
    });


    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response){
            errorHandle(response);

        }


    });

return false;

});


});

function errorHandle(error)
{
    console.log(error);
}

This is the file that will eventually modify the DOM if there is a PHP error. For now, it just logs.

checkLogin.php

if($verifySqlRequest==1){
    session_register("loginUsername");
    session_register("loginPassword");
    header("location:http://localhost/asd/dashboard.html");
    echo "login success!";  
}
else{
    echo "Wrong user name or password...";
}

This is just a snippet of the login authentication, but essentially I want that last echo to be communicated to controller_login.js, but it should continue the redirect if the login is successfully authenticated. Any help would be much appreciated!, thanks!

Upvotes: 0

Views: 4265

Answers (3)

Neil Girardi
Neil Girardi

Reputation: 4923

The point of using Ajax is to avoid reloading the page. If the user is going to be redirected upon login you may as well just submit the form synchronously and let PHP display the error.

Just for the sake of argument, if you wanted to do an Ajax function and display a message returned from the server you would NOT do:

<?php 
echo 'this is the message'; 
?>

You would do

<?php
$response = array('message' => 'this is my message');
return json_encode($response);
?>

However, you can't redirect AND return data.

Upvotes: 0

or you can do it like this

> echo '<script>window.location.replace("http://localhost/asd/dashboard.html")</script>';

and

 $.ajax({
url: url,
type: type,
data: data,
success: function(response){
    $('#response_element').html(response);

}

Upvotes: 2

Justin Helgerson
Justin Helgerson

Reputation: 25541

The browser isn't going to respond to a 302 or 301 (redirect) from an Ajax call. You can still certainly respond with that code, but, you'll have to handle the redirect manually via script:

window.location.replace("http://localhost/asd/dashboard.html");

Upvotes: 2

Related Questions