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 :
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.
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 ?