php - 通过 POST 提交时删除某些字符

标签 php html

我有两个问题

  1. 当我提交角色 ' 时通过我的 HTML 表单(使用 POST)就可以了。但是,在表单中,我允许修改提交的内容,当它被引入时,'之后的任何内容。消失。我推断这是因为当我分配包含 ' 的文本内容时到文本字段,它会关闭引用。例如,如果我在那里提交“Hello,我是 John”,则会执行: <input type=text value='Hello there I'm Jon /> 所以你看,I'm 中的撇号关闭 value 属性的引用。因此,我能想到的唯一解决方案是转义撇号,但即使我将 mysql_real_escape_string() 函数留在内容上(因为它被提交到数据库,并为此表单进行转义和检索)。
  2. 同样,当我提交 & 时或+ ,它消失了。每当我尝试在任何地方打印它时,无论使用 htmlspecialchars() ,都会发生这种情况。函数(我的印象是应该将这些字符编码为 HTML 格式,例如: &amp; )。举个例子,如果有人输入 Me & you那么它将显示为 Me you .

所以我问:如何解决上述问题,似乎与特殊字符有关,尽管它们已经转义了(我什至尝试再次应用转义函数)?如果我应该提供任何示例代码,请告诉我,但我已经解释了我对每个输入所做的事情。

最佳答案

When I submit the character ' through my HTML form (using POST) it is fine. However, in the form I allow to modify the submitted content, when it is brought in, anything after the ' disappears. I've deduced that this is because when I assign the text content containing the ' to the text field, it closes the quote. For example, if I submit Hello there I'm John, it will do: <input type=text value='Hello there I'm Jon /> So you see, the apostrophe in I'm closes the quote for the value attribute. So the only solution I can think of would be to escape the apostrophe, but even when I leave my mysql_real_escape_string() function on the content (as it's submitted to a database escaped and retrieved for this form).

这与提交数据无关。您正在尝试使用'在以 ' 分隔的属性值中字符。

使用 htmlspecialchars($data, ENT_QUOTES)

Similarly, when I submit an & or a +, it disappears. This happens any time I try to print it anywhere, regardless of using the htmlspecialchars() function (which I was under the impression should encode them in HTML format for such characters, like: &). so as an example, if someone enters Me & you then it will be displayed as Me you.

在编码为 application/x-www-form-urlencoded & 的数据中表示“新键=值对的开始”和 +意思是“一个空间”。您需要 urlencode($data) .

关于php - 通过 POST 提交时删除某些字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9501359/

相关文章:

html - 盒子大小的替代方法

php - 我应该选择 AJAX 和 JQuery 的复杂性和速度还是 GET 的简单性?

php - 在 pure-ftpd 中使用 PHP 的 password_hash

php - 在 Ajax for php 中序列化数组

javascript - 动态更新 src 时 videojs youtube 插件 MEDIA_ERR_SRC_NOT_SUPPORTED

html - 使用 SVG 的动画虚线

javascript - 动态加载带有选项卡的表单

php - 如何从 MySQL 记录发送自动电子邮件?

PHP 从日志事件中获取行号

html - 为什么我的 div 表在调整大小时失去了底部边框?