如何在AJAX中处理PHP函数()返回值?

时间:2016-02-07 08:54:05

标签: javascript php ajax function return-value

我试图在Ajax的帮助下使用PHP插入HTML表单数据, 我编写的代码如下 HTML代码:

    <!DOCTYPE HTML>
<html lang="en">
<head><title>Ajax Test</title>
    <meta charset="utf-8" name="viewport" content="width=device-width initial-scale=1.0">
<script>
function exec(){
    var name=document.getElementById("name").value;
    var uname=document.getElementById("uname").value;
    var xtr=new XMLHttpRequest();
    xtr.onreadystatechange=function(){
        if(xtr.readyState==4 && xtr.status==4){
            document.getElementById("p_res").innerHTML=xtr.responseText;
        }
    };
    xtr.open("GET","insert.php?name="+name+"&uname="+uname,true);
    xtr.send(null);
}
</script>
</head>
<body>
<form>
    Name : <input type="name" id="name"><br>
    Username : <input type="uname" id="uname"><br>
    <button type="button" onclick="exec()">Submit</button>
</form>
<div id="p_res"></div>
</body>
</html>

和相应的php页面是它..它返回一些值但是Ajax代码不会打印然后在为该代码分配的指定位置..我该怎么做才能解决这个错误..

<?php
class insert
{
    /**
     * insert constructor.
     * @param $name
     * @param $uname
     */
    function __construct($name, $uname)
    {
        $conn = pg_connect("host=localhost dbname=test user=postgres password=password");
        if (!$conn) {
            return "Error, Could not connect!";
        }
        $query = "INSERT into test(uname,name) VALUES ('$uname','$name')";
        $res = pg_query($conn, $query) or die("Can not exec Query...");
        return (<<<ret
Data Inserted Successfully...
ret
        );
    }
}

/** @var TYPE_NAME $obj_test */
$obj_test=new insert($_GET['name'],$_GET['uname']);
?>

请支持我们因为我是ajax的新手,我对ajax没有任何好主意......                     谢谢你们......

2 个答案:

答案 0 :(得分:0)

不是试图从构造函数返回一个值,而是使用另一个函数,如下所示。

class insert{
    /**
     * insert constructor.
     * @param $name
     * @param $uname
     */

    private $res;

    function __construct($name, $uname)
    {
        $conn = pg_connect("host=localhost dbname=test user=postgres password=password");
        if (!$conn) {
            return "Error, Could not connect!";
        }
        $query = "INSERT into test(uname,name) VALUES ('$uname','$name')";
        $this->res = pg_query($conn, $query) or die("Can not exec Query...");
    }


    public function showsuccess(){
        echo $this->res ? 'Data Inserted Successfully...' : 'Error inserting data';
    }
}

$obj_test=new insert($_GET['name'],$_GET['uname']);
$obj_test->showsuccess();
$obj_test=null;

答案 1 :(得分:0)

您的代码中存在一些问题。首先,构造函数方法不返回任何内容。因此,而不是return使用echo,而不是:{/ p>

<?php
    class insert
    {
        function __construct($name, $uname)
        {
            $conn = pg_connect("host=localhost dbname=test user=postgres password=password");
            if (!$conn) {
                echo "Error, Could not connect!";
            }
            $query = "INSERT into test(uname,name) VALUES ('$uname','$name')";
            $res = pg_query($conn, $query) or die("Can not exec Query...");
            if($res){
                echo "Data Inserted Successfully";
            }else{
                echo "Data could not be inserted";
            }
        }
    }

    $obj_test=new insert($_GET['name'],$_GET['uname']);
?>

第二,看看你的onreadystatechange事件:

xtr.onreadystatechange=function(){
                                ^ you are not catching the response text
    if(xtr.readyState==4 && xtr.status==4){
                                        ^ status should be 200
        document.getElementById("p_res").innerHTML=xtr.responseText;
    }
};

所以你的exec()函数应该是这样的:

function exec(){
    var name=document.getElementById("name").value;
    var uname=document.getElementById("uname").value;
    var xtr=new XMLHttpRequest();
    xtr.onreadystatechange=function(responseText){
        if(xtr.readyState==4 && xtr.status==200){
            document.getElementById("p_res").innerHTML=xtr.responseText;
        }
    };
    xtr.open("GET","insert.php?name="+name+"&uname="+uname,true);
    xtr.send(null);
}