Angularjs& PHP提交,POST和GET

时间:2017-02-13 15:36:44

标签: javascript php html sql angularjs

我需要有关此代码的帮助。

当我点击其中一个元素时

<li ng-repeat="mesa in mesas" ng-click="select($index,mesa.table_number)">
  <div class="round1" ng-if="selectedIndex === $index">&nbsp;</div>
  <div class="round" ng-if="selectedIndex !== $index">&nbsp;</div>
  MESA {{mesa.table_number}}</li>

angularjs的代码发布其值。

$scope.select = function (index, id){
    $scope.selectedIndex = index;
    $scope.options = true;
    $scope.idhttp = id;
    $http({
      method: 'POST',
      url: '../../php/getDetalles.php',
      data: {
        'mesa' : $scope.idhttp
      }
    }).then(function successCallback(data) {
        $scope.detalles = data
    }, function errorCallback(data) {
        alert('No Response');
    });

然后使用元素编号i的帖子尝试获取此查询的结果。

PHP

<?php
include('base.php');
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$msfu = $request->mesa;
$data = array();
$result = mysql_query("SELECT name, price FROM orders WHERE table_number LIKE '$msfu'",$connect);
if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_assoc($result)){
        $data = $row;
    }
} else {
    echo "0 results";
};
echo json_encode($data);
mysql_close($connect);?>

那么(更多的角色)

$http({
    method: 'GET',
    url: '../../php/getDetalles.php',
    }).then(function successCallback(data) {
        $scope.detalles = data,
        $scope.cuentas = data
    }, function errorCallback(data) {
        $scope.mesas = 'No Response';
    });

HTML

<div id="pedidos_table">
  <div id="pedidos_table_item" ng-repeat="detalle in detalles">
    <p id="pedidos_table_item_name">{{detalle.name}}</p>
    <p id="pedidos_table_item_price">{{detalle.price}}$</p>
  </div>
</div>

我试图在$scope.idhttp上发布号码1。但即使在查询中我手动插入值1,代码也会让我复制字段并在其上打印任何内容(空字段)。我有没有人能帮助我?

2 个答案:

答案 0 :(得分:0)

$ http承诺的.then方法使用response对象解析,而不是data

$scope.select = function (index, id){
    $scope.selectedIndex = index;
    $scope.options = true;
    $scope.idhttp = id;
    $http({
      method: 'POST',
      url: '../../php/getDetalles.php',
      data: {
        'mesa' : $scope.idhttp
      }
    //}).then(function successCallback(data) {
    }).then (function onSuccess(response)
        //DATA is property of response object
        var data = response.data;
        $scope.detalles = data
    }, function errorCallback(data) {
        alert('No Response');
    });

有关详细信息,请参阅AngularJS $http Service API Reference - General Usage

答案 1 :(得分:0)

解决了,我在$ data = $ row之后在php代码上添加了一个[]。所以它就像这样结束了。 PHP

<?php
include('base.php');
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$msfu = $request->mesa;
$data = array();
$result = mysql_query("SELECT name, price FROM orders WHERE table_number = '$msfu'",$connect);
if(mysql_num_rows($result) > 0){
    while($row = mysql_fetch_assoc($result)){
        $data[] = $row;
    }
} else {
    echo "0 results";
};
echo json_encode($data);
mysql_close($connect);?>

ANGULARJS

$http({
      method: 'POST',
      url: '../../php/getDetalles.php',
      data: {
        'mesa' : $scope.idhttp
      }
    }).then (function onSuccess(response){
        var data = response.data;
        $scope.detalles = data
    }, function errorCallback(data) {
        alert('No Response');
    });

$http({
          method: 'GET',
          url: '../../php/getDetalles.php'
        }).then(function successCallback(response) {
            $scope.detalles = response.data,
            $scope.cuentas = response.data
        }, function errorCallback(response) {
            $scope.entradas = 'No Response';
        });

感谢所有

@lin和@georgeawg。