Ajax和PHP post请求返回空值

时间:2013-05-29 03:53:58

标签: php ajax post elrte

我正在尝试将textarea(elRTE)的内容发送到php,看看我正在尝试的内容,

的index.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>

然后下面是ajax函数,它使用post请求或发送数据到abc.php文件......

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' 有人请帮忙

0 个答案:

没有答案