簡體   English   中英

Angularjs和PHP提交,POST和GET

[英]Angularjs & PHP submitting, POST and GET

我需要有關此代碼的幫助。

當我單擊此元素之一時

<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');
    });

因此,然后使用元素編號的帖子,我試圖獲取此查詢的結果。

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);?>

則(更多ANGULARJS)

$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,代碼也使我能夠復制字段,而在字段上不打印任何內容(空字段)。 我為此瘋了,有人可以幫助我嗎?

$ http promise的.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服務API參考-常規用法

解決后,我在$ 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM