如何处理 PHP function() return 中 AJAX 的值?

how to handle PHP function() return value in AJAX?

我正在尝试在 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 页面是它.. 它 return 一些值但是 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 没有任何好主意... 谢谢大家....

与其尝试 return 来自构造函数的值,不如为此目的使用另一个函数,如下所示。

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;

您的代码中几乎没有问题。首先,构造方法没有 return 任何东西。所以不要使用 return,而是使用 echo,像这样:

<?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);
}