I am performing asynchronous HTTP (Ajax) requests via jQuery with the basic $.ajax(). The code looks like the following:
$("textarea").blur(function(){
var thisId = $(this).attr("id");
var thisValue = $(this).val();
$.ajax({
type: "POST",
url: "some.php",
data: "id=" + thisId + "&value=" + thisValue,
success: function(){
alert( "Saved successfully!" );
}
});
});
Everything is working properly as usual, until user types inside textarea ampersand (&) character. Than when I debug PHP function, which saves the value, it always have a value until this character.
I believe there has to be a solution to skip ampersand (&) somehow. Any ideas?
Etymology. The term ampersand is a corruption of and (&) per se and, which literally means "(the character) & by itself (is the word) and." The symbol & is derived from the ligature of ET or et, which is the Latin word for "and."
An ampersand (&) is a typographical symbol that is rarely used in formal writing. It is read aloud as the word and and is used as a substitute for that word in informal writing and in the names of products or businesses.
What is an &? & is called an ampersand symbol (pronounced “AM- per-sand”). Essentially, it means “and”. It is used both (a) in the body of the paper as part of a citation and (b) at the end of the paper as part of a reference.
It is actually a ligature (two characters combined) an e and t from the Latin word et, meaning “and”. With many variations, there are two main ways of writing the ampersand: the traditional version (&), and the style that looks more like an E or an et.
Instead of:
data: "id=" + thisId + "&value=" + thisValue
do:
data: { id: thisId, value: thisValue }
This way jquery will take care of properly URL encoding the values. String concatenations are the root of all evil :-)
Strongly recommend you use the solution provided by Darin above if at all possible; that way, you get to reuse well-tested code for building POST
data.
But if you really, really, really need to use string concatenation (here, or elsewhere in your application when building up query strings or POST
data out of user inputs), you need to use encodeURIComponent
:
$("textarea").blur(function(){ var thisId = $(this).attr("id"); var thisValue = $(this).val(); $.ajax({ type: "POST", url: "some.php", data: "id=" + encodeURIComponent(thisId) + "&value=" + encodeURIComponent(thisValue), success: function(){ alert( "Saved successfully!" ); } }); });
By default when sending a POST
with jQuery.ajax
, you're sending data with the content type application/x-www-form-urlencoded
, which means you're promising that the data is encoded that way. You have to be sure to keep your part of the bargain and actually encode it. This isn't just important for ampersands.
just use the javascript function encodeURIComponent()
:
$("textarea").blur(function(){
var thisId = $(this).attr("id");
var thisValue = $(this).val();
$.ajax({
type: "POST",
url: "some.php",
data: "id=" + thisId + "&value=" + encodeURIComponent(thisValue),
success: function(){
alert( "Saved successfully!" );
}
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With