How to redirect user to another page after submitting Ajax form

I am having trouble redirecting the user to a thank you page after successfully filling out the form. The following happens: after submitting the form, it goes to a blank page ( https://cunet.sparkroom.com/Sparkroom/postLead ) ... I need to redirect it to the page with thanks when submitting the form information in the URL in the "action form".

HTML code:

<form action="https://cunet.sparkroom.com/Sparkroom/postLead/" method="post" name="theForm" id="theForm" onSubmit="return MM_validateForm();" > ... </form> 

Ajax Code:

 <script src="http://malsup.github.com/jquery.form.js"></script> <script> $(document).ready(function() { $('#theForm').ajaxForm(function() { alert('form was submitted'); }); success:function(response) { location.window.href = "redirect user to the thank you page"; } }); </script> 

JavaScript:

 function MM_validateForm() { if ( !jQuery('#theForm #FirstName').val() ) { alert('Please input your first name.'); jQuery('#theForm #FirstName').focus(); return false; } if ( !jQuery('#theForm #LastName').val() ) { alert('Please input your last name.'); jQuery('#theForm #LastName').focus(); return false; } if ( !jQuery('#theForm #daytimephone').val() ) { alert('Please input your phone number.'); jQuery('#theForm #daytimephone').focus(); return false; } if ( !jQuery('#theForm #Email').val() ) { alert('Please input your email.'); jQuery('#theForm #Email').focus(); return false; } if ( !jQuery('#theForm #BID').val() ) { alert('Please select your preferred campus.'); jQuery('#theForm #BID').focus(); return false; } if ( !jQuery('#theForm #programs').val() ) { alert('Please select your preferred program.'); jQuery('#theForm #programs').focus(); return false; } if ( !jQuery('#theForm #How_Heard').val() ) { alert('Please select how you heard about us.'); jQuery('#theForm #How_Heard').focus(); return false; } return true; } // ]]></script> 

Does anyone know what I'm doing wrong? I need a form to send data to a URL, and then after redirecting the user to the page with thanks

+8
source share
7 answers

The success callback syntax is incorrect. It should be:

 $('#theForm').ajaxForm(function() { window.location.href = "/path/to/thankyoupage"; }); 

Also note that this is window.location.href and not location.window.href

+15
source

This is jQuery Solution:

 window.location("www.example.com"); 
+1
source
 //Please try this <script src="http://malsup.github.com/jquery.form.js"></script> <script> $(document).ready(function() { $('#theForm').ajaxForm(function() { alert('form was submitted'); }); success:function(response) { if(response){ // check whether response is received location.window.href = "http://your_domain_name/thank-you";} } }); </script> 
+1
source

You have to use

 window.location.href = "http://www.google.com"; 

but not

 location.window.href = "http://www.google.com"; 

http://www.w3schools.com/js/js_window_location.asp

Success should be a function passed as an argument to $ .ajaxForm:

 $('#theForm').ajaxForm(function() { window.location.href = "redirect user to the thank you page"; }); 
0
source

The following code worked for me in a completely different OP context:

 $(document).ready(function() { window.location.href = "URL"; }); 
0
source

You can try like this:

  success: function (data) { window.location.href = data.redirecturl; }, error: function () { alert('error happened'); } 
0
source

Must be

 window.location = "http://www.google.com"; 
-2
source

Source: https://habr.com/ru/post/1492321/


All Articles