tags:

views:

158

answers:

3

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!

A: 

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.

Sohnee
urlencode is supported javascript?
jball
Are you sure that urlencode exist in javascript? Use encodeURIComponent()!
systempuntoout
I tried "escape" and it works just fine :)
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.
David Dorward
Yes, I was doing a lot of PHP that day - in JavaScript it is called encodeURIComponent!
Sohnee
+2  A: 

Try calling encodeURIComponent in your javascript when posting the request.

jball
I tried "escape" and it works just fine :)
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.
David Dorward
cool, I'm using the one you suggest..what special characters does 'escape' omit?
`@`, `+`, `/` ... Check this page out: http://www.the-art-of-web.com/javascript/escape/ especially the table in section 3
jball
A: 

Check this SO question that explain how to encode an url in javascript.

systempuntoout