How to get value contentEditable in PHP

半城伤御伤魂 提交于 2019-12-20 03:00:36

问题


How we can get the content present in contentEditable in html format in PHP

For example i have this code and want to get its value in a variable in PHP:

<div contentEditable="true"> type here
<img src="http://t2.gstatic.com/images?q=tbn:ANd9GcQCze-mfukcuvzKk7Ilj2zQ0CS6PbOkq7ZhRInnNd1Yz3TQzU4e&t=1" /></div>

​ because i want to use this as html body for an email.

Infact i want to get the value of contentEditable and save it into a variable and then use that variable as a body for my email. Any idea how to perform this task.


回答1:


You'll need to store the contents of the element in a form element so it can be submitted to your PHP page.

I assume you want contenteditable instead of textarea for rich text... consider taking a look at WYSIWYG editors like TinyMCE instead.

If you still want to stick with your current method, you'll need some JS to copy the value of the div into a hidden form element before the page gets submitted.

HTML

<div id="emailcontent" contenteditable>asdf</div>
<form>
  <input type="text" id="a" name="a"/>
  <input type="submit" name="submit" id="submit" />
</form>

jQuery

$("#submit").click(function() {
  $("#a").val($("#emailcontent").html());
});

DEMO




回答2:


You can try something like this:

var form = document.createElement("form");
form.setAttribute('action','some.php');
form.setAttribute('method','POST');
form.setAttribute('target','_self');
form.setAttribute('enctype','application/x-www-form-urlencoded');
form.innerHTML = '<input type="hidden" name="emailData" value="'+
                     document.getElementById('yourdivid').innerHTML+'" />';
form.submit();

Then in your php retrieve POSTed data:

$data = $_POST['emailData'];



回答3:


If you want to use PHP maybe you can try to use php DomDocument this is some example maybe same with want you want

How can I get a div content in php

How to get a div via PHP?

or you can use javascript and jquery to do that



来源:https://stackoverflow.com/questions/11526675/how-to-get-value-contenteditable-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!