文本区域值未出现在 $_POST 对象上

发布于 2024-11-14 04:31:24 字数 312 浏览 2 评论 0原文

伙计们,我对此感到摸不着头脑。

我有这个网站,它基本上包含一些由用户填写的表格。然后,用户可以以人类可读格式(pdf)或机器可读格式(xml)下载该信息,但我在提交文本框时遇到了一个小问题。

我有其中一些,例如在描述部分,但是当我访问 $_POST['Desc_Desc_desc'] 值时,它是空的,即使我可以看到文本区域上的内容。奇怪的是,当我使用 firebug 检查元素时,它显示该元素就好像它没有内容一样。

任何人都可以弄清楚是什么导致了这种奇怪的行为吗?

Guys, im scratching my head around this one.

I have this website that basically contains a few forms that are filled in by the user. The user then can download that information in human readable format (pdf) or machine readable format (xml) but I'm having a slight problem submitting textboxes.

I have a few of them, for instance in the description section, but when i access the $_POST['Desc_Desc_desc'] value, it's empty even though i can see content on the textarea. The weird thing is that when i use firebug to inspect the element, it shows the element as if it had no contents..

Can anyone figure what is causing this strange behavior?

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

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

发布评论

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

评论(4

做个ˇ局外人 2024-11-21 04:31:24

service_level_library.buttons.prepForSubmit 中,文本区域与表单的其余部分一起通过 DOM cloneNode 方法。这会复制 HTML 元素属性,但不会复制 DOM 属性。 (有时 DOM 元素节点属性有一个相应的属性< /a>,因此更新属性会影响属性,这可能会使 DOM 属性看起来正在被复制。)

textarea DOM 对象 具有 value 属性textarea HTML 元素 没有相应的 value 属性,因此 value property 没有公开属性。因此,当您克隆节点时,(空)value 属性将被复制,留下 元素的当前值(可通过 value 属性访问)。

要修复脚本,请执行以下操作之一:

  1. 克隆后复制值。
  2. 设置初始值文本区域,通过分配给 defaultValue 属性或在克隆之前设置节点的文本内容。这是有效的,因为克隆节点的当前值将设置为其初始值,并且文本区域的深层副本将复制其文本内容(其初始值的来源)。
  3. 在克隆之前以编程方式将文本区域替换为输入(尽管这比其他选项更复杂),

In service_level_library.buttons.prepForSubmit, the textarea is cloned along with the rest of the form via the DOM cloneNode method. This copies HTML element attributes, but not DOM properties. (Sometimes DOM element node properties have a corresponding attribute, so updating the property affects the attribute, which can make it appear that DOM properties are being copied.)

While textarea DOM objects have a value property, the textarea HTML element doesn't have a corresponding value attribute, so the value property isn't exposing an attribute. Thus when you clone the node, the (empty) value attribute is what gets copied, leaving the current value of the element (as accessible via the value property) behind.

To fix your script, do one of:

  1. Copy the value after cloning.
  2. Set the initial value for the textarea, either by assigning to the defaultValue property or setting the text content of the node, before cloning. This works because the current value of the cloned node will be set to its initial value, and a deep copy of a textarea will copy its text contents (the source of its initial value).
  3. Programmatically replace the textarea with an input before cloning (though this would be more involved than the other options),

尽管在代码中我看到一个名为 Dep_desc 和 id Dep_Desc_Desc 的文本区域,但你在问题中说 $_POST['Desc_Desc_desc'] 。那么你应该写$_POST['Dep_desc'],即

另外,textarea 没有 value 属性,因此在 html 中,您应该在开始标记和结束标记之间写入初始内容。

HTML

<textarea name="Dep_desc" id="Dep_Desc_Desc">Initial content</textarea>

PHP

echo "The content of the textarea is ".nl2br(htmlspecialchars($_POST['Dep_desc']));

注释

nl2br:尊重 html 输出中替换 \n 符号与

htmlspecialchars:防止可能的XSS攻击。

You say in your question $_POST['Desc_Desc_desc'], although in the code I see a textarea with name Dep_desc and id Dep_Desc_Desc. Then you should write $_POST['Dep_desc'], i.e. the name of the <textarea> instead of the id.

Also, textareas don't have a value attribute, so in your html you should write the initial content between the opening and the closing tag.

HTML

<textarea name="Dep_desc" id="Dep_Desc_Desc">Initial content</textarea>

PHP

echo "The content of the textarea is ".nl2br(htmlspecialchars($_POST['Dep_desc']));

notes

nl2br: Respect new lines in the html output replacing the \n symbol with <br />.

htmlspecialchars: Prevent possible XSS attacks.

鸵鸟症 2024-11-21 04:31:24

我使用 firebug 来分析发送到脚本的内容

document.getElementById('descriptionForm').submit()

表单数据正在正确发送

内容类型:application/x-www-form-urlencoded 内容长度:113

des_des_name=so&des_des_keywords=overflow&des_des_concept=http%3A%2F%2Fso.com&des_des_desc=Stack+overflow

  1. 当前设置表单操作参数到“submit.action”,它会产生 404。
  2. 表单按钮是在表单标记之外定义的。
  3. 表单按钮不会激活表单上的提交。

底线:表单不会提交给预期的收件人。

I used firebug to analyze what gets sent to your script by

document.getElementById('descriptionForm').submit()

The form data is being sent correctly

Content-Type: application/x-www-form-urlencoded Content-Length: 113

Desc_Desc_name=SO&Desc_Desc_keywords=overflow&Desc_Desc_concept=http%3A%2F%2Fso.com&Desc_Desc_desc=Stack+overflow

  1. The form action parameter is currently set to "submit.action" which yields a 404.
  2. The form buttons are defined outside your form tag.
  3. The form buttons does not activate a submit on your form.

Bottom line: the form does not get submitted to the intended recipient.

丢了幸福的猪 2024-11-21 04:31:24

我遇到了类似的问题,但后来我发现在底部的某个地方,我对另一个空元素重复使用了相同的名称。这也消除了 var_dump、print_r 中我所需的元素。花了一段时间才把它弄出来。

I had a similar issue but later I found out that somewhere at the bottom, I had reused the same name for another element which was empty. That wiped off my required element at var_dump, print_r as well. It took a while to get that out.

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