Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  25] [ 1]  / answers: 1 / hits: 5441  / 10 Years ago, tue, july 1, 2014, 12:00:00

I have the following code:



  var result = confirm(You want to Subscribe to our Newsletter?);
var emailAddress = $(#subscribeEmail).val();

if (result == true) {

$.ajax({
type: 'POST',
url: '/php/subscribeNewsletter.php',
data: '{email: ' + emailAddress + '}',
complete: function(r){
alert(r.responseText);
}

});


}


I believe the problem is to do with:
data: '{email: ' + emailAddress + '}',



I am receiving an empty $_POST array on the server side of things.


More From » jquery

 Answers
1

Pass an object literal, not a string:



data: {email: emailAddress },


jQuery will transform the object into the URL encoded key/value pairs, which will be picked up in the $_POST array on the PHP side.



Your current code is actually sending a JSON string as the raw POST data. jQuery is seeing the data type is a string, and so it doesn't do any processing, so you'd have to access the raw POST data on the PHP side and do a JSON decode to get it.


[#44196] Monday, June 30, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rashawn

Total Points: 451
Total Questions: 83
Total Answers: 83

Location: Egypt
Member since Tue, May 3, 2022
2 Years ago
rashawn questions
;