如何将div的内容作为POST参数发送? [英] How can I send contents of a div as a POST parameter?

查看:221
本文介绍了如何将div的内容作为POST参数发送?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

<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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆