2

I'm looking to make a Contact/Query form, wherein the end user can send an email to the webmaster. The form has a 'textarea' field, which captures long strings from the user, if I use AJAX to submit the form using GET method, my params tend to break if their is a special character, specifically '&' in the textarea's string.. I'm stuck please help!

  • 2
    Isn't this an abuse of HTTP/Rest to use GET to post data to the server? – Greg K Mar 22 '10 at 10:41
  • Check [this](http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript) SO question that explain how to encode an url in javascript. – systempuntoout Mar 22 '10 at 10:55

2 Answers2

3

Try calling encodeURIComponent in your javascript when posting the request.

jball
  • 24,791
  • 9
  • 70
  • 92
  • 1
    Don't use `escape`, it doesn't "work just fine" (unless your data *happens* to fall into the subset of characters which it handles OK). Use encodeURIComponent. – Quentin Mar 22 '10 at 11:01
  • cool, I'm using the one you suggest..what special characters does 'escape' omit? –  Mar 22 '10 at 11:08
  • `@`, `+`, `/` ... Check this page out: http://www.the-art-of-web.com/javascript/escape/ especially the table in section 3 – jball Mar 22 '10 at 15:39
0

You can make the string URL safe using JavaScript urlencode...

var textToSend = encodeURIComponent(myform.myfield.value);

This will convert all special characters into URL encoded characters.

Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    Are you sure that urlencode exist in javascript? Use encodeURIComponent()! – systempuntoout Mar 22 '10 at 10:52
  • Don't use `escape`, it doesn't "work just fine" (unless your data *happens* to fall into the subset of characters which it handles OK). Use encodeURIComponent. – Quentin Mar 22 '10 at 11:00
  • Yes, I was doing a lot of PHP that day - in JavaScript it is called encodeURIComponent! – Fenton Mar 23 '10 at 10:31