PHP$\u在<;中获取内容;部门>;
首先,我是新来的,如果我犯了错误,请告诉我^” 我试着用php$\u POST获取div元素的内容,但我真的不知道怎么做。如果这很重要的话,我会使用MVC模式。 我的div是可编辑的,答案框可以在页面上写东西PHP$\u在<;中获取内容;部门>;,php,Php,首先,我是新来的,如果我犯了错误,请告诉我^” 我试着用php$\u POST获取div元素的内容,但我真的不知道怎么做。如果这很重要的话,我会使用MVC模式。 我的div是可编辑的,答案框可以在页面上写东西 <form action="index.php?page=addPost&topic_id={TOPIC_ID}" method="post"> <div id="editor" name="editor"> Lorem Ipsum
<form action="index.php?page=addPost&topic_id={TOPIC_ID}" method="post">
<div id="editor" name="editor">
Lorem Ipsum...
</div>
<input type="submit" name="submit" value="senden" >
</form>
这可能吗?使用ajax或只使用textarea表单元素
<form id="data" method="post" enctype="multipart/form-data">
<div id="editor" name="editor">
Lorem Ipsum...
</div>
<input type="submit" name="submit" value="senden" >
<input type="hidden" id="topicId" value="{TOPIC_ID}" >
</form>
<script>
$(document).ready(function(){
$("form#data").submit(function(){
var formData = new FormData($(this)[0]);
formData.append('editor', $('#editor').html());
$.ajax({
url: 'index.php?page=addPost&topic_id='+$('$topicId').val(),
type: 'POST',
data: formData,
async: false,
success: function (data) {
alert(data);
location.reload();
},
cache: false,
contentType: false,
processData: false
});
return false;
});
});
</script>
乱数假文。。。
$(文档).ready(函数(){
$(“表单数据”).submit(函数(){
var formData=新formData($(此)[0]);
append('editor',$('#editor').html();
$.ajax({
url:'index.php?page=addPost&topic_id='+$('$topicId').val(),
键入:“POST”,
数据:formData,
async:false,
成功:功能(数据){
警报(数据);
location.reload();
},
cache:false,
contentType:false,
processData:false
});
返回false;
});
});
以下是如何继续:
<script>
$(document).ready(function(){
$("#myform").on("submit",function(e){
e.preventDefault(); //Prevent default form submission
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
if (xmlhttp.responseText == "true") {
//success message
} else {
//error message
}
}
}
xmlhttp.open("POST", "index.php", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //to maintain HTML format though you pass value through POST
xmlhttp.send("page=addPost&topic_id="+$('#topicID').val()+"value=" + $('#editor').html());
});
});
</script>
$(文档).ready(函数(){
$(“#我的表格”)。关于(“提交”,功能(e){
e、 preventDefault();//防止默认表单提交
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4&&xmlhttp.status==200){
if(xmlhttp.responseText==“true”){
//成功信息
}否则{
//错误消息
}
}
}
open(“POST”,“index.php”,true);
setRequestHeader(“Content-Type”,“application/x-www-form-urlencoded”);//通过POST传递值来维护HTML格式
xmlhttp.send(“page=addPost&topic_id=“+$('#topicID').val()+”value=“+$('#编辑器').html());
});
});
对您的表单进行小的修改:
<form id="myform" method="post">
<div id="editor" name="editor">
Lorem Ipsum... in WYSIWYG format
</div>
<input type="hidden" id="topicID" value="{TOPIC_ID}"/>
<input type="submit" name="submit" value="senden" />
</form>
Lorem Ipsum…所见即所得格式
您可以使用隐藏的文本区域,并在表单提交时将div的内容添加到文本区域:
<form action="index.php?page=addPost&topic_id={TOPIC_ID}" method="post" onsubmit="getEditorContents(this);">
<div id="editor">
Lorem Ipsum...
</div>
<textarea style="display:none;" name="editor"><!-- --></textarea>
<input type="submit" name="submit" value="senden" >
</form>
<script>
function getEditorContents(form){
var html = document.getElementById("editor").innerHTML;
form.editor.value = html;
return true;
}
</script>
乱数假文。。。
函数GetEditorContent(表单){
var html=document.getElementById(“编辑器”).innerHTML;
form.editor.value=html;
返回true;
}
您需要AJAX way-JQuery以$('editor')的形式提交编辑器。valueA div不是表单元素,因此您需要使用一些javascript。您目前是否使用js库(例如JQuery)?或者只使用textarea
表单元素如果有一些原因你不能使用textarea元素,除了:每当编辑div时,使用javascript将内容放入
我从现在起就没有使用过Ajax或jquery ^^,所以我不知道如何使用它。但是谢谢你,似乎我必须学习它。如果你在使用jquery,为什么要使用冗长的ajax请求呢?为什么不$.post($(this.attr('action'),{editor:$('#editor').html()},function(){…})我更喜欢这种方式,因为我个人感觉好多了。顺便说一下,这种方法可以帮助初学者了解实际的处理过程
<form action="index.php?page=addPost&topic_id={TOPIC_ID}" method="post" onsubmit="getEditorContents(this);">
<div id="editor">
Lorem Ipsum...
</div>
<textarea style="display:none;" name="editor"><!-- --></textarea>
<input type="submit" name="submit" value="senden" >
</form>
<script>
function getEditorContents(form){
var html = document.getElementById("editor").innerHTML;
form.editor.value = html;
return true;
}
</script>