我正在尝试将 textarea(elRTE) 的内容发送到 php,请看下面我正在尝试的内容,
索引.php
<script>
......
$('#btnsub').click(function() {
var content = $('#editor').elrte('val');
var title = document.getElementById('atitle').val;
var tag = document.getElementById('atag').val;
//window.open("abc.php?cont="+content);
fetchData(content);
})
})
</script>
然后下面是使用 post 请求将数据发送到 abc.php 文件的 ajax 函数...
Ajax.js
function fetchData(id)
{
if(x)
{
//alert(id);
//var path="homepagedata.php?dataId="+id;
//x.open("GET",path);
alert(id);
var url = "abc.php";
var param= "name="+id;
x.open("POST", url, true);
//Send the proper header information along with the request
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
x.send(param);
//x.setRequestHeader("Content-length", parameters .length);
//x.setRequestHeader("Connection", "close");
x.onreadystatechange=function()
{
if(x.readyState==4)//&& x.status==200)
{
alert(x.responseText);
//document.getElementById("showData").innerHTML=x.responseText;
}
}
x.send(null);
}
}
这是 abc.php 文件 abc.php
<?php
$name= $_POST['name'];
echo $name;
?>
现在,当我运行此代码时,我得到空白警报,即没有通过帖子发送数据,所以请告诉我上面的代码有什么问题......
编辑:
问题解决了:
function fetchData(id)
{
if(x)
{
//alert(id);
//var path="homepagedata.php?dataId="+id;
//x.open("GET",path);
alert(id);
var url = "abc.php";
var param= "name="+id;
x.open("POST", url, true);
//Send the proper header information along with the request
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
x.setRequestHeader("Content-length", parameters .length);
x.setRequestHeader("Connection", "close");
x.send(param);
x.onreadystatechange=function()
{
if(x.readyState==4 && x.status==200)
{
alert(x.responseText);
//document.getElementById("showData").innerHTML=x.responseText;
}
}
x.send(null);
}
}
而php是,
<?php
include("conn.php")
$name= $_POST['name'];
$query=mysql_query("insert into aw_about_us(abt_content) values('$name')")or die(mysql_error());
if($query==true) {
echo "success";
}else
echo "fail";
?>
但是现在警报显示实际文本,它应该显示“成功还是失败”?实际上问题没有解决它显示警报,因为我提醒'id'有人请帮助