如何将特殊/保留字符从 HTML 表单发布到 PHP 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11886954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to post special/reserved characters from HTML forms to PHP pages?
提问by Chris Headleand
I have a form that looks like this:
我有一个看起来像这样的表格:
<form action="/assesment/savelist/" method="post">
<input type="hidden" name="owner" value="<?php echo $userid ?>" />
<input type="text" name="title" value="Question List Title" />
<textarea name="description"></textarea>
<input type="submit" />
</form>
In the description people will have to be able to use the £character (among other non-allowed characters).
在描述中,人们必须能够使用该£字符(以及其他不允许使用的字符)。
Is there anyway to convert these characters to something that is allowed before posting them to my PHP page?
在将这些字符发布到我的 PHP 页面之前,有没有将它们转换为允许的内容?
Hi All, thanks for your comments so far.
大家好,感谢您到目前为止的评论。
If I do print_r($_POST) on my "savequestion" it outposts the postdata that gets sent to it from that form.
如果我在“savequestion”上执行 print_r($_POST) ,它会输出从该表单发送给它的 postdata。
however, if there is a £ in any of the fields then that specific character doesnt get sent. For example if I was to post "sdfsdfs £ adasd" from that form all that would get sent is "sdfsdfs adasd"
但是,如果任何字段中有 £,则不会发送该特定字符。例如,如果我要从该表格中发布“sdfsdfs £ adasd”,那么所有将被发送的是“sdfsdfs adasd”
the question is how do I convert the £ to something that I can send as post data from a HTML form.
问题是如何将 £ 转换为我可以从 HTML 表单作为发布数据发送的内容。
回答by Chris Headleand
WIN!
赢!
The solution is to add accept-charset="utf-8" to the form tag.
解决办法是在form标签中添加accept-charset="utf-8"。
I didnt have the option to add this to the header of the page but adding it to the form tag solved all my issues. Big shout out to @deceze for posting a link to this website http://kunststube.net/frontback/
我没有将其添加到页面标题的选项,但将其添加到表单标签解决了我所有的问题。大喊@deceze 发布到这个网站的链接http://kunststube.net/frontback/
回答by Quentin
Browsers will automatically encode data when it is submitted via the standard form submit mechanism.
当数据通过标准表单提交机制提交时,浏览器将自动编码数据。
PHP will automatically decode data when it populates $_POST/GET/REQUEST.
PHP 将在填充数据时自动解码数据$_POST/GET/REQUEST。
You don't need to do anything at that stage.
在那个阶段你不需要做任何事情。
You might need to encode the data before inserting it into a database / some HTML / an email / a URI / some other data format, but that would depend on what you are doing with the data.
在将数据插入数据库/一些 HTML/电子邮件/URI/其他一些数据格式之前,您可能需要对数据进行编码,但这取决于您对数据的处理方式。
回答by user7461775
Browsers will 'automatically' "encode" <> data when it is submitted via the standard form submit mechanism.
当通过标准表单提交机制提交时,浏览器将“自动”“编码”<> 数据。

