使用php将数据从角度js发送到数据库

时间:2018-03-24 06:07:21

标签: php angularjs

我正在尝试根据angularjs中选定的下拉列表获取过滤数据。我从数据库成功获取选项,当我直接传递虚拟(测试)选项时,php确实返回过滤数据。剩下的唯一障碍是如何将实际选择的选项从下拉列表传递到php文件?我已经检索了所选的选项。

test.php(包含角度下拉列表):

<select value="center_name" ng-model="selectedname" ng-options="name.center_name for name in namelist" ng-change="loadcounsellors(selectedname)">
        <option >Please select a name</option>  
    </select>

data.js:

var app=angular.module('myapp',[]);
app.controller('MyCtrl',function($scope,$http,$httpParamSerializer)
{
  $http.get("logintype.php")
  .then(function(response)
{   
  $scope.namelist=response.data;

})
$scope.loadcounsellors=function(item){
  alert(item.center_name);
 $http.post("counselorlist.php",$httpParamSerializer({center_name:item.center_name})) 

  .then(function(response)
{
  // alert("hello world");
  $scope.counsellor=response.data;
  alert(response.data);

})
}
})

             <h2>Selected: {{counsellor}}</h2>   

counselorlist.php:

<?php 

require "connection.php";
// $postdata = file_get_contents("php://input");
// $params = json_decode(file_get_contents('php://input'), true);
// $request1 = json_decode($postdata);
// echo $request1;
$center_name = $_POST['center_name'];
echo $center_name;
// $center_name = "Andheri"; //This works fine.
$query = "select distinct(edited_by) from telephonic_enquiry
 where center_name='$center_name'";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result)>0 ) {
    $response=array();
    while ($row= mysqli_fetch_array($result)) {
        array_push(
            $response, array (
            'edited_by'=>$row['edited_by'],
            )
        );
    }
    echo json_encode($response);
}
mysqli_close($conn);
?>

1 个答案:

答案 0 :(得分:0)

我这样修好了以防其他人需要它:

$http.post("counselorlist.php",  {"center_name":item.center_name})