0
我正在使用AngularJS並嘗試將params傳遞給php程序,所以php程序可以使用一個或多個變量進行選擇。AngularJS http post到PHP程序使用Json數組
我需要傳遞一個json數組,它是我的sql綁定變量,它的where_clause如下。
這是Main.php
<!DOCTYPE html>
<html >
<style>
table, th , td {
border: 1px solid grey;
border-collapse: collapse;
padding: 5px;
}
table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
<body>
<div ng-app="myApp" ng-controller="customersCtrl">
<table>
<tr ng-repeat="x in names">
<td>{{ x.Name }}</td>
<td>{{ x.Country }}</td>
</tr>
</table>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http)
{
var req = {
method: 'POST',url: 'angular_master.php',
headers: {
'Content-Type': undefined
},
params: { what_to_do: "angular_users6", where_clause: '[{"sqlvalue1":"searchvalue1","sqlvalue2":"searchvalue2"}]'}
}
$http(req).then(function (response) {$scope.names = response.data.records;});
});
</script>
</body>
</html>
這是angular_master.php
if ($what_to_do == "angular_users6")
{
$json = $where_clause;
$where_array = json_decode($json,true);
error_log("\n where_clause_json : " . print_R($json,TRUE) ,3,$filename_master);
error_log("\n where_clause_array: " . print_R($where_array,TRUE) ,3,$filename_master);
$sqlvalue1 = "";
$sqlvalue2 = "";
foreach($where_array as $rows)
{
$sqlvalue1 = mysqli_real_escape_string($conn, $rows['sqlvalue1']) ;
$sqlvalue2 = mysqli_real_escape_string($conn, $rows['sqlvalue2']);
}
error_log("\n where_array: " . $sqlvalue1 . " " . $sqlvalue2 ,3,$filename_master);
我得到的日誌在下面的部分,我的陷阱 LOG:
where_clause_json : [{"sqlvalue1":"searchvalue1","sqlvalue2":"searchvalue2"}]
where_clause_array: Array
(
[0] => Array
(
[sqlvalue1] => searchvalue1
[sqlvalue2] => searchvalue2
)
)
where_array:
問題是最後一個echo語句「where_array:」(該行上面的行沒有t顯示sqlvalue1和sqlvalue2的值,儘管print_R回顯顯示值。
由於傳遞的where_clause可能包含多個值,爲什麼最後一個echo where_array爲空。
for工作正常,我用它在移動應用程序中。 –