Recently I am developing a rich text editor. Considering that textarea can only input text, I use the contenteditable="true" attribute of p to implement rich text, which can insert pictures, videos, etc.
But here comes the problem, on the form page:
<form action="test.php" method="post">
<p contenteditable="true" name="zhengwen"></p>
<input type="submit">
</form>
In this way, the content in p cannot be submitted to the test.php page at all, and echo $_POST[zhengwen]; is not output.
I suspect it was not submitted at all.
Does anyone know how to solve it? Looking for simple codes for form submission page and receiving page! ! !
PHP中文网2017-07-04 13:48:04
It is indeed not submitted because the submit button in the form only submits the form elements. And <p contenteditable="true">
is not a form element.
If you want to submit the information of <p contenteditable="true">
, you need to construct the POST
request yourself. I will use jQuery as an example:
$('form').submit(function(e) {
e.preventDefault(); // 拦截HTML的默认表单提交
var content = $('p[name=zhengwen]').html();
$.post('....', {zhengwen: content}, functino(data) {
// 成功回调
});
});
Of course, there are many ways to write jQuery's POST submission, such as $.ajax()
, etc.
When receiving on the PHP side, just read $_POST
as usual.
欧阳克2017-07-04 13:48:04
1、富文本编辑器可以用百度的UEditor
2、正如楼上所说,你可以用ajax来提交,但是这个有一点不好,如果有一百个输入框,难道提交一百个键值对?
3、所以你可以用js的formData对象,图片也可以发送过去,代码如下
$("#submit").click(function() {
var x = new FormData(document.getElementById("frm"));//构造方法里面必须是dom对象
x.append('abc', 123);//追加你的数据
$.ajax({
url: '1.php',
type: 'POST',
data: x,
processData: false, // 告诉jQuery不要去处理发送的数据
contentType: false // 告诉jQuery不要去设置Content-Type请求头
})
.success(function(data) {
//代码
});
});
4、也可以用serializeArray函数模拟上面的formData对象,代码如下
var allDatas = $("form").serializeArray();
allDatas.push({name:'data',value: JSON.stringify(你的数据对象)});//追加的格式必须是name,value形式,打印allDatas的格式就知道了!!!
$.post(url,allDatas,function(json){//代码
});