views:

198

answers:

2

I'm using Jquery to submit the value of a <textarea> through AJAX to a PHP script. This value may contain punctuation characters (!,?,#,@) as well as single and double quotes, and other non-alphanumeric characters. Does Jquery take care of encoding all this stuff or do I need to do it myself? How can I be certain that whatever the user typed in the <textarea> would be submitted?

+1  A: 

Yes, jquery will care about that. but always add the correct encoding to the page you are browsing at. in my case I try to use always utf-8

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Gabriel Sosa
+2  A: 

I use encodeURIComponent for that. If there's a risk the text contains a '%'-sign, i replace that with it's htmlcode (&#something) before encoding.

KooiInc
encodeURIComponent will correctly encode % to %25
Greg
Doesn't jquery automatically do that?
Click Upvote
@Greg, you're right. I remember having had problems with %, but could be long time ago.@Click Upvote, Sorry, I don't use jquery.
KooiInc
@Click Upvote again: check http://malsup.com/jquery/form/comp/. At the bottom it says: "Each of the libraries tested use JavaScript's encodeURIComponent function to encode form data." So theoretically using jQuery, you wouldn't need encodeURIComponent?
KooiInc