Reputation: 582
I am trying to post an email address using ajax to php and every time I send the email address variable gets cut off, so it is sent to the php as '[email protected]' for example. I have tried doing toString and escape but nothing seems to work.
Thanks.
function postEmail(){
var checkEmail = "[email protected]";
var dataString = 'checkEmail1='+ checkEmail;
// AJAX Code To Submit Form.
$.ajax({
type: "POST",
url: "myfile.php",
data: dataString,
cache: false,
success: function(result){
alert(result);
}
});
}
Upvotes: 1
Views: 1415
Reputation: 1870
This is the recommended way to build your jQuery AJAX request data:
var dataString = {checkEmail1: checkEmail};
See the examples in the documentation.
It is especially useful when you are posting multiple values, such as this:
var data = {
something1 : yourVariable1,
something2 : yourVariable2,
something3 : 'hardcodedvalue'
}
I much prefer this over doing manual string concatenation and individual encoding of the data like you are doing.
Upvotes: 0
Reputation: 582
Shortly after posting this, after trying for ages, I got it working.
I used encodeURIComponent and didn't even need to decode it the other end:
var checkEmail = "[email protected]";
checkEmail = encodeURIComponent(checkEmail); //added this in
var dataString = 'checkEmail1='+ checkEmail;
Upvotes: 2