将换行符/回车符作为隐藏字段值发布

发布于 2024-07-14 23:37:56 字数 233 浏览 7 评论 0原文

我需要通过隐藏字段发布多行数据。 发布后数据将在文本区域中查看。 如何在 html 表单中发布换行符/回车符?

我已经尝试过 \r\n 但这只是发布实际的“\r\n”数据

<input type="hidden" name="multiline_data" value="line one\r\nline two" />

有没有办法做到这一点?

I need to post multi-line data via a hidden field. The data will be viewed in a textarea after post. How can I post a newline/carriage return in the html form?

I've tried \r\n but that just posts the actual "\r\n" data

<input type="hidden" name="multiline_data" value="line one\r\nline two" />

Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

懒的傷心 2024-07-21 23:37:57

虽然新行(回车和换行)在技术上允许在 隐藏状态下使用,但为了与旧版浏览器兼容,应该对它们进行转义。 您可以通过替换所有回车符(\u000D\r)和所有换行符(\u000A\n),其中包含被应用程序识别为回车符或换行符的专有字符串(如果存在于原始字符串中,也会进行转义)。

简单的字符实体在这里不起作用,因为不合格的浏览器可能知道 是新行并剥离他们从价值。

示例

例如,在 PHP 中,如果您要将传递的值回显到文本区域,则需要包含换行符(和未转义的字符串)。

但是,在 PHP 中,如果您要将值回显value 属性, 标记时,您可以使用专有字符串(例如 \r\n)转义新行,并转义提交值中专有字符串的任何实例。

然后,在其他地方使用该值之前(插入数据库、发送电子邮件等),如有必要,请务必对提交的值进行转义。

再次保证

为了进一步保证,我询问了 WHATWG,目前 HTML 规范的编辑 Ian Hickson 回答道:

bfrohs关于的问题 -- 值中是否允许换行和回车? 它们在文本状态和搜索状态下是明确禁止的,但没有提及隐藏状态。 如果没有,是否有一个可接受的 HTML 解决方案来存储文本区域中的表单数据?

Hixie 是的,它们是允许的 // iirc // 由于遗留原因,您可能希望转义它们,尽管某些浏览器将它们标准化 // 我忘记了我们是否修复了该问题 //规格

来源

While new lines (Carriage Return & Line Feed) are technically allowed in <input>'s hidden state, they should be escaped for compatibility with older browsers. You can do this by replacing all Carriage Returns (\u000D or \r) and all Line Feeds (\u000A or \n) with proprietary strings that are recognized by your application to be a Carriage Return or New Line (and also escaped, if present in the original string).

Simply character entities don't work here, due to non-conforming browsers possibly knowing and are new lines and stripping them from the value.

Example

For example, in PHP, if you were to echo the passed value to a textarea, you would include the newlines (and unescaped string).

<textarea>Some text with a \ included
and a new line with \r\n as submitted value</textarea>

However, in PHP, if you were to echo the value to the value attribute of an <input> tag, you would escape the new lines with your proprietary strings (e.g. \r and \n), and escape any instances of your proprietary strings in the submitted value.

<input type="hidden" value="Some text with a \\ included\r\nand a new line\\r\\n as submitted value">

Then, before using the value elsewhere (inserting into a database, emailing, etc), be sure to unescape the submitted value, if necessary.

Reassurance

As further reassurance, I asked the WHATWG, and Ian Hickson, editor of the HTML spec currently, replied:

bfrohs Question about <input type=hidden> -- Are Line Feeds and Carriage Returns allowed in the value? They are specifically disallowed in Text state and Search state, but no mention is made for Hidden state. And, if not, is there an acceptable HTML solution for storing form data from a textarea?

Hixie yes, they are allowed // iirc // for legacy reasons you may wish to escape them though as some browsers normalise them away // i forget if we fixed that or not // in the spec

Source

躲猫猫 2024-07-21 23:37:57

确实取决于字符集,但是 应该换行并且 应该是回车。 您应该能够在 value 属性中使用它们。

Depends on the character set really but should be linefeed and should be carriage return. You should be able to use those in the value attribute.

好多鱼好多余 2024-07-21 23:37:57

您没有说明这是做什么的,也没有说明您正在使用什么技术,但您需要注意,您不能相信隐藏字段会保留 value="line one

第二行”,因为恶意用户可以在 POST 发回之前对其进行篡改。由于您稍后将值放入

值写入隐藏字段并读回时,通常最好将其作为会话的属性保留在服务器上。 ,或页面流,或您的环境提供的任何执行此类操作的内容。

You don't say what this is for or what technology you're using, but you need to be aware that you can't trust the hidden field to remain with value="line one line two", because a hostile user can tamper with it before it gets sent back in the POST. Since you're putting the value in a <textarea> later, you will definitely be subject to, for example, cross site scripting attacks unless you verify and/or sanitize your "multiline_data" field contents before you write it back out.

When writing a value into a hidden field and reading it back, it's usually better to just keep it on the server, as an attribute of the session, or pageflow, or whatever your environment provides to do this kind of thing.

情栀口红 2024-07-21 23:37:56

而不是使用

尝试使用

Instead of using

<input type="hidden">

Try using

<textarea style="visibility:hidden;position:absolute;">

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文