I have a problem concerning the input of special characters. Firstly, + is considered
as a special character, right? I have a field in my form that needs to be able to contain a +. However, when I send it to my PHP script using AJAX and access the variable using $_POST, the + is not displayed, and therefore, not saved in the database.
Example:
// on the JavaScript side
value = +123;
paramPost = "name=abc&value=" + value;
alert("paramPost = " + paramPost);
// Output: parampost = name=abc&value=123
// The + is gone!
// Also, when I change value to a string, the + is still there,
// but when PHP receives it, it's gone.
ajax.doPost(paramPost);
// on the PHP side
$val = $_POST['value'];
echo "val = $val";
// Output: 123
// The + is no longer there!
What can I do to fix this?
I tried this:
$val = htmlspecialchars($_POST['value'], ENT_QUOTES);
…but it still didn’t work.
A
+is redundant on a number; change+123to"+123".If your JavaScript library does not escape that, also do
encodeURIComponent(value)instead ofvalue. So, your fixed code should be: