AngularJS http post to PHP program using Json array



我正在使用AngularJS并尝试将参数发布到php程序中,以便php程序可以使用一个或多个变量进行选择。

我需要传递一个 json 数组,这是我的 sql 绑定变量,它是下面的where_clause。

这是主要的.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:  

问题是最后一个回显语句"where_array:"(这一行上方的行不显示sqlvalue1和sqlvalue2的值,尽管print_R回显显示值。

由于传递的where_clause可能包含多个值,为什么最后一个回显where_array空白。

看来你的foreach是错误的。where_arrays的格式是:

where_arrays = [["sqlvalue1" => webcastpoa@thepentecostals.org, "sqlvalue2" => na]]。

尝试 foreach($where_array[0] 作为 $rows(

最新更新