I have a textarea:
<textarea id="my_text"></textarea>
And have some ajax code in javascript like this:
var xmlhttp = new XMLHttpRequest();
var my_text = document.getElementById("my_text").value;
xmlhttp.open("POST", ".", true);
xmlhttp.send("my_text=" + my_text + "&foo=1");
When I filled the textarea with string abc +1 +2 +3, and run the JS code. The value of “my_text” I get in the server side is abc 1 2 3.
How can I change the JS code to get the right string abc +1 +2 +3 in server side.
Use
encodeURIComponent(my_text)to prepare your text for submission. This function should always be used for parameters, to properly escape characters. You have already experienced an occasion where your code breaks.Another aweful result of not escaping the characters occurs when the user uses a
&in his string, effectively creating an extra parameter.