How to redirect user to another page after Ajax form submission?

15,719

Solution 1

Try

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

Solution 2

Try:

$('#theForm').ajaxForm(function() { 
 success : function(){
    window.location.href = "Url to redirect here in the success option";
 }
});
Share:
15,719

Related videos on Youtube

June
Author by

June

Updated on June 04, 2022

Comments

  • June
    June almost 2 years

    I'm having problems redirecting the user to a thank you page after a successful form completion. What happens is that after the form submits, it goes to a blank page (https://cunet.sparkroom.com/Sparkroom/postLead)... I need it to redirect to a thank you page while submitting the form details to the URL in the 'form action'.

    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> 
        $('#theForm').ajaxForm(function() { 
        window.location.href = "I want the user to be redirected to this 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 the form to submit the data to the URL and then after redirect the user to a 'thank you' page, right now that is not at all happening.

  • June
    June almost 11 years
    How would I be able to do option 1? Do you have any code to provide examples with?
  • nks
    nks almost 11 years
    Add these line in your validation function. var url = "cunet.sparkroom.com/Sparkroom/postLead"; var data = jQuery('#theForm').serialize(); jQuery.ajax( { url : url, type : 'post', data : data, success : function() { window.location.href = "thankyou.com"; } });
  • partizanos
    partizanos over 8 years
    This implies HTTP Get request form submission is Post.
  • antonavy
    antonavy about 7 years
    Nice! That worked like a charm for me! :) After reading 10+ questions with saving windows and alike. Thanks!