Tim Aych
Tim Aych

Reputation: 1365

Jquery Ajax POST being appended to url

I fixed one thing in my code that broke my ajax submit. Not sure what's going wrong here. The PHP has been unchanged since everything was working. For some reason, and I'm unsure if this is supposed to happen or not, the datastring is appended to the url. The alert box never appears.

$('#contact_submit').click(function(e){
    var datastring = 'name='+ $('#name').val() + '&email=' + $('#email').val() + '&inquiry='+ $('#inquiry_dropdown option:selected').text() + '&message='+ $('#message').val();
    $.ajax({  
        type: "POST",  
        url: "process-contact.php",  
        data: datastring,  
        success: function(data) {  
            alert(data);
        }  
    });
    alert ("We've received your request and will alert you once the directory is available. Thank you.");
    $('#submit_form_contact').reset();
});  
//    if ($("#submit_form_contact").valid()) {
//    }
});

Upvotes: 0

Views: 1254

Answers (2)

Adam Spence
Adam Spence

Reputation: 3240

Try using Jquery's serialize method instead of building the data manually

Upvotes: 1

developerCK
developerCK

Reputation: 4506

Dear you are sending a datastring that will append in url, instead of send it like

var datastring = 'name='+ $('#name').val() + '&email=' + $('#email').val() + '&inquiry='+ $('#inquiry_dropdown option:selected').text() + '&message='+ $('#message').val();

send it like

var datastring = {"name" : $('#name').val(),
                  "email": $('#email').val() 
                    .......
                     }

Upvotes: 1

Related Questions