2016-02-07 63 views
0

我試圖使用Ajax的幫助下插入使用PHP的HTML表單數據, 我寫的代碼如下 的html代碼:如何處理AJAX中的PHP函數()返回值?

<!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']); 
?> 

請支持我的人,因爲我是新對於阿賈克斯,我對阿賈克斯沒有任何好的想法...... T hank的傢伙....

+0

對不起你們我想在這裏放一些信息..數據庫中的數據插入成功,但沒有返回任何值...我的意思是指出是顯示在HTML頁面regride重新spective database success message .. –

+2

類構造函數不返回值,它們返回類對象的實例。 – RamRaider

+0

簡單的建議,把它用於你將要使用'onclick'屬性是不必要的。請參閱:https://en.wikipedia.org/wiki/Unobtrusive_JavaScript –

回答

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; 
0

您的代碼中幾乎沒有問題。首先,構造函數方法不返回任何東西。因此,而不是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); 
}