duansai1314 2016-09-09 19:14
浏览 88

Http从Angular获取请求到PHP Server问题

How to create the GET request with parameter and pass the parameter to PHP page and using that value to create a dynamic SELECT where query and populate the response in angular UI ? Let you explain the code and other details briefly.

sample.js

var link = 'http://www.testhostfile.com/GetData.php';

$http({
url: link,
method: "GET",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: $scope.inmateData.IMID

}).then(function (res){
alert('Success');
console.log(res);
});

GetData.php

<?php

$db_name  = 'testDb';
$hostname = 'localhost';
$username = 'testName';
$password = 'password';
$usertable = "InMateMaster";    

$fileData = file_get_contents("php://input");
$request = json_decode($fileData);

if (isset($request)) {

$imID = $request->IMID;

echo($imID);
}

$db = new mysqli("localhost",$username,$password,$db_name);

if($db->connect_errno > 0) {
die('Unable to connect to database[' .$db->connect_error. ']');
}

$sql = "SELECT * FROM InMateMaster WHERE IMID = 'KAS9791257393'";


if(!$result = $db->query($sql)){
die('There was an error running the query [' .$db->error. ']');
}

$arr = array();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$item = array();
$item['IMName'] = $row['IMName'];
$item['IMMobile'] = $row['IMMobile'];
$item['IMFatherName'] = $row['IMFatherName'];
$arr[] = $item;
}
}

header('Content-Type: application/json');
echo json_encode($arr);

?>

Now How to assign the dynamic value which i have passed from GET request parameter to here like below:

$sql = "SELECT * FROM InMateMaster WHERE IMID = 'here i need to set'";

As per below query, I received the response like in the below snap.

$sql = "SELECT * FROM InMateMaster WHERE IMID = 'KAS9791257393'";

Response :

Output

1.So, How can I using select query dynamically - in the sense How can I pass one value from GET request and in that value how to query using SELECT and WHERE and how to populate the result in angular.

2.As per below response in the snap. How can I get those values in angular any idea ?

[{"IMName":"Venu M","IMMobile":"9791257393","IMFatherName":"Murugan N"}]

Let me know, If you need anymore clarifications in this.

  • 写回答

1条回答 默认 最新

  • doushi2845 2016-09-10 06:30
    关注

    I used below way to achieve the data from angular to PHP via Get request as per Mr.Marc suggestion it's working well.

    sample.js

    $http.get('http://www.hostelkasthuri.com/GetInmate.php?'+$scope.inmateData.IMID
                          ).success(function(response) {
                                  alert("SUCCESS!");
                                  console.log(response.IMName);
                                  })
                         .error(function(data) {
                                alert("ERROR");
                                alert(data);
                                });
    

    sample.php

    $imID= $_SERVER['QUERY_STRING'];
    echo "The query string is: " . $imID;
    

    $sql = "SELECT * FROM InMateMaster WHERE IMID = '$imID'";

    My question here is - As per above workout Now can able to get the response from PHP using dynamic query.

    Response :

    {"IMName":"Suthan M","IMMobile":"9659057393","IMFatherName":"Murugan N"}

    Now, How can I retrieve above format of response into angular js any idea ?

    My working code is here but how to get each value in angular response block ?

    $http.get('http://www.hostelkasthuri.com/GetInmate.php?'+$scope.inmateData.IMID
                      ).success(function(response) {
                              alert("SUCCESS!");
                              console.log(response.IMName);
                              })
                     .error(function(data) {
                            alert("ERROR");
                            alert(data);
                            });
    

    response.IMName - getting null why ?

    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能