Skip to content Skip to sidebar Skip to footer

Ajax Form Redirect After Submit

Hello there i'm having problem with redirection after form has been submit. I'm using simple ajax form for displaying login errors in a div. $(document).ready(function(){ $('#forml

Solution 1:

If you redirect a page that was called with AJAX, it will not redirect the parent page.

What you should do is echo something that identifies that the user has successfully logged in.

Maybe instead of header('Location: logged_in.php'); you put:

echo "success";

Then, in your success function:

success: function(data) {
    if (data == "success")
        window.location = "logged_in.php";
    else
        alert("Wrong password.");
}

Note that success in ajax does not mean that you have successfully logged in, just means that the request to the page returned without error (500). You still need to decide what you want to do with your data.


Solution 2:

There are two different things here.

1 - Your AJAX function. It is on a page that call the server asynchronously

2 - Your login page. It is called by your page containing the AJAX

The code in your second page happens in the server, while your AJAX code is happening in your client.

You should return a success message in your php login page and then, in your success callback, check if the message is the correct one and that will mean the user logged in successfully. Once you check the message is the correct one, you can redirect the user from your success callback, doing: location.href = "yourUrl"


Solution 3:

Here is some production code that works great for me, to submit the form and then redirect.

$.ajax({
        url: "QuoteReload.php",
        type: 'GET',
        data: 'batchid=' + bid + '&operation=archiveBatch',
    success: function(response) { 
                // window.location.href = "EditItem.php?batchid=" + bid + "&quoteitemid=NEW";
                $(location).attr('href', 'Quote.php?QuoteId=<?php echo $QuoteId; ?>')
            },
    error: function(xhr) {
            $(ddl).css("background-color", "#4C25BC");
            alert('Delete Batch error: ' + xhr); 
             }
});

Post a Comment for "Ajax Form Redirect After Submit"