今天用php做博客系统的时候遇到了一个问题。首先,在编辑博客的js控件是用的别人开源的js插件,然后封装了获取里面内容的js函数,获取的是html格式的内容,这样可以记录字体大小、类型....到时候发布的时候更加方便。遇到的问题就是用js获取了新闻发布的内容怎么用post方式将数据传递到另一个php文件中,百度了很久,有些人用的是ajax的方式,后来发现了一种直接在js中虚拟一个form表单,然后虚拟一些控件,并给这些控件赋值(就是你要传递的数据),最后提交表单就好了!
var ue = UE.getEditor('editor');
function post(URL, PARAMS) {
var form = document.createElement("form");
form.action = URL;
form.method = "post";
form.style.display = "none";
for(var x in PARAMS){
var text = document.createElement("textarea");
text.name = x;
text.value = PARAMS[x];
form.appendChild(text);
}
document.body.appendChild(form);
form.submit();
return form;
}
function getContent() {
var article_content = [];
var article_title = document.getElementById("article_title").value;
article_content.push(ue.getContent());
article_content.join("\n");
post("do_add_article.php", {article_title:article_title, article_content:article_content});
}