【发布时间】:2011-06-09 03:15:43
【问题描述】:
我有一个关于特殊字符输入的问题。首先,考虑+
作为一个特殊的角色,对吧?我的表单中有一个字段需要能够包含+。但是,当我使用 AJAX 将其发送到我的 PHP 脚本并使用 $_POST 访问变量时,+ 不会显示,因此不会保存在数据库中。
例子:
// 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!
我能做些什么来解决这个问题?
我试过了:
$val = htmlspecialchars($_POST['value'], ENT_QUOTES);
...但还是不行。
【问题讨论】:
-
请清理您的代码并使用代码标签。 //parampost = name=abc&value=+123 应该被注释掉吗?有关系吗?
-
我对 php 了解不多,但是没有什么办法可以逃避字符吗?
-
嗨,火柴!谢谢伙计,已经得到了解决方案..我只是把它写出来作为基础:) 我正在使用 encodeURIComponent() 并且它工作正常。谢谢你的回复:)
标签: php javascript ajax special-characters http-post