Want to redirect to the URL after successfully submitting the form JS

user7475534

Want to redirect to the URL after successfully submitting the form JS. Here is my code. I have 2 different forms, and I need to redirect them to another URL. Want to redirect to the URL after successfully submitting the form JS. Here is my code. I have 2 different forms, and I need to redirect them to another URL. Want to redirect to the URL after successfully submitting the form JS. Here is my code. I have 2 different forms, and I need to redirect them to another URL.

$(document).ready(function() {
    $(function() {
        var form = $('#ajaxContact');
        var formMessages = $('#contactMessage');
        $(form).submit(function(e) {
            e.preventDefault();
            if (jQuery(".email", this).val() == '' && jQuery(".message", this).val() == '' && jQuery(".name", this).val() == '') {
                $(formMessages).text('Required fields must be filled');
                return false;
            }
            var formData = $(form).serialize();
            formData += '&subjects=New Contact Us Message&phone="None"&required=email,name,message';
            $.ajax({
                    type: 'POST',
                    url: $(form).attr('action'),
                    data: formData
                })
                .done(function(response) {
                    $(formMessages).removeClass('error');
                    $(formMessages).addClass('success');
                    // Set the message text.
                    $(formMessages).text(response);
                    jQuery(".email", this).val('');
                    jQuery(".name", this).val('');
                    jQuery(".message", this).val('');
                })
                .fail(function(data) {
                    // Make sure that the formMessages div has the 'error' class.
                    $(formMessages).removeClass('success');
                    $(formMessages).addClass('errors');
                    // Set the message text.
                    if (data.responseText !== '') {
                        $(formMessages).text(data.responseText);
                    } else {
                        $(formMessages).text('Oops! An error occured and your message could not be sent.');
                    }
                });
        });
    });
    $(function() {
        var form = $('#callMe');
        var formMessages = $('#callMeMessage');
        $(form).submit(function(e) {
            e.preventDefault();
            if (jQuery(".email", this).val() == '' && jQuery(".phone", this).val() == '') {
                $(formMessages).text('One of the filds must be filled');
                return false;
            }
            var formData = $(form).serialize();
            formData += '&name=Call Me Service&subjects=New "Call Me" request from Webzool site function&message=No Message&required=subjects,name';
            console.log(formData);
            $.ajax({
                    type: 'POST',
                    url: $(form).attr('action'),
                    data: formData
                })
                .done(function(response) {
                    $(formMessages).removeClass('error');
                    $(formMessages).addClass('success');
                    // Set the message text.
                    $(formMessages).text(response);
                    jQuery(".email", this).val('');
                    jQuery(".phone", this).val('');
                })
                .fail(function(data) {
                    // Make sure that the formMessages div has the 'error' class.
                    $(formMessages).removeClass('success');
                    $(formMessages).addClass('errors');
                    // Set the message text.
                    if (data.responseText !== '') {
                        $(formMessages).text(data.responseText);
                    } else {
                        $(formMessages).text('Oops! An error occured and your message could not be sent.');
                    }
                });
        });
    });

I would be very happy if you can help. Thanks

Christheoreo

Within your .done function you can add :window.location.href = 'webURL'; or window.location.replace("http://stackoverflow.com");

This will ensure that it will only redirect if the ajax call has been successful.

Example with your code:

$.ajax({
  type: 'POST',
  url: $(form).attr('action'),
  data: formData
})
.done(function(response) {
  $(formMessages).removeClass('error');
  $(formMessages).addClass('success');
  // Set the message text.
  $(formMessages).text(response);
  jQuery(".email", this).val('');
  jQuery(".phone", this).val('');
  window.location.replace("http://stackoverflow.com");
})

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I redirect users after successfully submitting form data?

How to prevent redirect after submitting data through a form

How to redirect confirmation page after submitting form in Laravel?

Redirect user to specific url after submitting the edit account address form

How to redirect to another HTML page after submitting a form?

I want to open modal after submitting form?

React Router redirect after submitting a form using the input value

How to redirect user after submitting form with React

How to redirect website after submitting google form?

Prevent Wrong Input in JS After Submitting A Form

After submitting my React form, is there an easier way of passing an object to the page I want to redirect to?

Trying to redirect in PHP after submitting an email form

Redirect to contacts page after submitting form in laravel 4

Redirect to URL after form post

Go to the url of the selection after submitting the form

How to redirect on submitting a form?

Open a popup after successfully submitting form

How to redirect after submitting form in php

Redirect to other view after submitting form

Redirect Url After Form Submission

redirect List<Data> to a view after user has submitting a form

In my spring login form url getting changed after submitting the form

I want to redirect the current page after submitting the form....it should return the page from where request is send using php

redirect to another page after submitting a form

PHPMAILER, how to redirect to the thank you page after submitting the form?

Redirect to function based detail view after submitting form

Django Form after redirect is empty after successfully submitting a form

"redirect" function of django is not and after submitting the form, user remains on same page

How to redirect after submitting form