Martyna va
Martyna va

Reputation: 181

Submit ajax form and redirect to other page using jquery

I'm having problem to submit form. It simply redirects to 'thank-you' page without sending form data first. Please help, code below:

$(function () {    
  $("#surveyControl").submit(function(e) {

  var companyname = $("#companyname").val();
  var firstname = $("#firstname").val(); 
  var lastname = $("#lastname").val();


  var dataString = '&companyname='+ companyname + '&firstname='+ firstname + '&lastname='+ lastname;

       e.preventDefault();

        $.ajax({
            type: "POST",
            url: "../wp-admin/emailer.php",
            data: dataString,
            async: false,
            success: function (data) {
               return true;
           }

window.location.href = "thank-you";
})

});

Upvotes: 0

Views: 1958

Answers (2)

Mustapha Larhrouch
Mustapha Larhrouch

Reputation: 3393

you need to redirects to 'thank-you' after ajax call success :

$.ajax({
            type: "POST",
            url: "../wp-admin/emailer.php",
            data: dataString,
            async: false,
            success: function (data) {
               window.location.href = "thank-you";
           }

Upvotes: 3

slash197
slash197

Reputation: 9034

window.location.href = "thank-you"; - this is cancelling your ajax request, do this when you got a response from ajax.

Upvotes: 0

Related Questions