如何将div的内容作为POST参数发送? [英] How can I send contents of a div as a POST parameter?
问题描述
我有此代码:
<div contenteditable="true"><p><?php echo $row[1]; ?></p></div>
我可以将div的内容作为POST参数发送,以便在PHP中使用它们.如果我可以使用,那就好了:onchange ="this.form.submit()".
Can I take the contents of the div and send them as a POST parameter in order to use them in the PHP. It would be good if I can use: onchange="this.form.submit()".
谢谢!
推荐答案
不可能发布div
标记的内容,因为这仅适用于表单元素.解决方法是在提交表单时使用一些Javascript填充hidden
字段,并改为发布hidden
字段.
It is not possible to post contents of div
tags, as this is only possible on form elements. The workaround for this would be to use some Javascript that populates a hidden
field when a form is submitted, and the hidden
field is posted instead.
观察以下HTML.看到在form
元素上附加了一个onsubmit
事件.我们在这里向浏览器说的是提交表单时,首先调用Javascript函数process
,并且仅在该函数返回true
时提交:
Observe the following HTML. See that there is an onsubmit
event attached to the form
element. What we're saying to the browser here is when the form is submitted, first call the Javascript function process
, and only submit if said function returns true
:
<form method="post" action="process.php" onsubmit="javascript: return process();">
<input type="hidden" id="hidden" name="content" vlaue="<?php echo $row[1] ?>">
<div contenteditable="true" id="content"><p><?php echo $row[1] ?></p></div>
<button type="submit">Post</button>
</form>
这将是您的Javascript.您正在执行的操作是获取ID为content
的元素的innerHTML
并将其分配给ID为hidden
的元素的value
并返回true
,以便可以成功提交表单:
This would be your Javascript. What you're doing is getting the innerHTML
of the element with the id content
and assigning it to the value
of the element with the id hidden
and return true
so the form can be successfully submitted:
<script>
function process() {
document.getElementById("hidden").value = document.getElementById("content").innerHTML;
return true;
}
</script>
在process.php
文件中,仅输出发布的内容:
And in the process.php
file, just output the posted content:
var_dump(发布的内容:".$ _POST ['content']);
var_dump("Posted content: " . $_POST['content']);
希望这会有所帮助!
这篇关于如何将div的内容作为POST参数发送?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!