Page redirect with successful Ajax request Page redirect with successful Ajax request jquery jquery

Page redirect with successful Ajax request


Sure. Just put something at the the end of your success function like:

if(result === "no_errors") location.href = "http://www.example.com/ThankYou.html"

where your server returns the response no_errors when there are no errors present.


Just do some error checking, and if everything passes then set window.location to redirect the user to a different page.

$.ajax({    url: 'mail3.php',    type: 'POST',    data: 'contactName=' + name + '&contactEmail=' + email + '&spam=' + spam,    success: function(result) {        //console.log(result);        $('#results,#errors').remove();        $('#contactWrapper').append('<p id="results">' + result + '</p>');        $('#loading').fadeOut(500, function() {            $(this).remove();        });        if ( /*no errors*/ ) {            window.location='thank-you.html'        }    }});


You can just redirect in your success handler, like this:

window.location.href = "thankyou.php";

Or since you're displaying results, wait a few seconds, for example this would wait 2 seconds:

setTimeout(function() {  window.location.href = "thankyou.php";}, 2000);