Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ampersand (&) character inside a value of jQuery AJAX request data option

Tags:

jquery

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?

like image 282
Nik Sumeiko Avatar asked Nov 19 '10 15:11

Nik Sumeiko


People also ask

Why is it called the ampersand?

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."

Is ampersand grammatically correct?

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 this symbol called &?

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.

How do you write an &?

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.


3 Answers

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 :-)

like image 58
Darin Dimitrov Avatar answered Sep 23 '22 11:09

Darin Dimitrov


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.

like image 26
T.J. Crowder Avatar answered Sep 22 '22 11:09

T.J. Crowder


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!" );
      }
   });

});
like image 39
js1568 Avatar answered Sep 19 '22 11:09

js1568