gpt4 book ai didi

php - AJAX请求中的特殊字符

转载 作者:行者123 更新时间:2023-11-28 21:21:22 25 4
gpt4 key购买 nike

我在输入特殊字符时遇到问题。首先考虑+作为一个特殊的 Angular 色,对吗?我的表单中有一个字段需要能够包含 +。但是,当我使用 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);

...但还是没用。

最佳答案

+ 在数字上是多余的;将 +123 更改为 "+123"

如果您的 JavaScript 库无法转义,也可以使用 encodeURIComponent(value) 而不是 value。所以,你的固定代码应该是:

value = "+123";
paramPost = "name=abc&value=" + encodeURIComponent(value);

// ..

ajax.doPost(paramPost);

关于php - AJAX请求中的特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6287734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com