I am posting the contents of a form field to a PHP script via AJAX and using JavaScript to escape (field_contents). The problem is that any plus signs are removed and replaced with spaces. How can I safely "encode" the plus sign and then "decode" it appropriately in PHP?
Try it in JavaScript:
PHP:
Use encodeuriccomponent() in JS and PHP and you should receive the correct value.
Note: When you access $_GET, $_POST, or $_REQUEST in PHP, you are retrieving an already decoded value.
Example:
In your JS:
On your server
Only the original HTTP request contains URL-encoded data.
For GET requests, you can retrieve it from the URI. $_SERVER['REQUEST_URI'] or $_SERVER['QUERY_STRING']. For urlencoded POST, file_get_contents('php://stdin')
Note:
Decode() only applies to single-byte encoded characters. It doesn't work for the entire UTF-8 range.
eg:
Note:
"Ā"
is equivalent to:escape('\xc4\x80')
This is the byte sequence representing Ā in UTF-8 (\xc4\x80). So if you use encodeuriccomponent(), your server side must know that it is receiving UTF-8. Otherwise PHP will mess up the encoding.