Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page redirect with successful Ajax request

I have a form that uses Ajax for client-side verification. The end of the form is the following:

$.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();              });          }     }); 

EDIT: this is my mail3.php file dealing with errors:

$errors=null;   if ( ($name == "Name") ) {     $errors = $nameError; // no name entered } if ( ($email == "E-mail address") ) {     $errors .= $emailError; // no email address entered } if ( !(preg_match($match,$email)) ) {     $errors .= $invalidEmailError; // checks validity of email } if ( $spam != "10" ) {     $errors .= $spamError; // spam error }  if ( !($errors) ) {     mail ($to, $subject, $message, $headers);     //header ("Location: thankyou.html");     echo "Your message was successfully sent!";     //instead of echoing this message, I want a page redirect to thankyou.html  } else {     echo "<p id='errors'>";     echo $errors;     echo "</p>"; } 

I was wondering if it's possible to redirect the user to a Thank You page if the ajax request is successful and no errors are present. Is this possible?

Thanks! Amit

like image 484
Amit Avatar asked Aug 18 '10 15:08

Amit


People also ask

How do I redirect to another view in ajax success?

How do I redirect to another view in ajax success? $. ajax({ type: 'POST', url: 'AJAX URL', data: “YOUR DATA” // don't forget to pass your csrf_token when using post success: function(data){ $(“what_ever_you_want_to_replace”). html(data.

How do I redirect a page in ajax?

ajax({ type: 'POST', url: 'AJAX URL', data: "YOUR DATA" // don't forget to pass your csrf_token when using post success: function(data){ $("what_ever_you_want_to_replace"). html(data. view); }, error: function(xhr, type, exception) { // if ajax fails display error alert alert("ajax error response type "+type); } });

Does ajax follow redirect?

ajax appears to always follow redirects.

What triggers ajax success?

What triggers Ajax success? Whenever an Ajax request completes successfully, jQuery triggers the ajaxSuccess event. Any and all handlers that have been registered with the . ajaxSuccess() method are executed at this time.


2 Answers

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.

like image 145
Adam Avatar answered Sep 19 '22 15:09

Adam


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'         }      } }); 
like image 22
TJ L Avatar answered Sep 16 '22 15:09

TJ L