【问题标题】:Posting array of input using AngularJs使用 AngularJs 发布输入数组
【发布时间】:2016-09-25 13:30:20
【问题描述】:

希望你一切安好

我在html中有以下表格

<div class="table-responsive " >
<table class="table table-bordered">
<tr ng-repeat="x in [1, 2, 3, 4, 5]">
<td>
<td align="center">Price: <input name="price[]" type="number"  ng-model="info.price[x]"/>
</td>
</tr>
</table>
</div>

我使用这个向插入函数提交数据:

ng-submit="addPrice(info)

这是我的功能:

$scope.addPrice= function(info){
info.price= [];
$http.post('../files/insert.php',{"price":info.price}).success(function(data){
if (data == true) {
}
});
}

在php文件中:

    $data = json_decode(file_get_contents("php://input"));     
    $price[]=mysqli_real_escape_string($con, $data->price);    
    $query = "INSERT into myTbl (price) VALUES ('$price[0]')";
    mysqli_query($con, $query);

如您所见,我正在尝试插入第一个价格输入,但无论我在输入中输入什么,它都会在数据库中插入 0!

问题出在哪里?!还是我做错了什么?

问候

【问题讨论】:

  • 为什么要在发布之前重新初始化info.price = []??以及您如何期望该数组中应该存在一些值?
  • 即使没有初始化,它仍然插入0

标签: php html angularjs forms http-post


【解决方案1】:

从以下部分删除info.price初始化,

$scope.addPrice= function(info){
    //info.price= [];
    $http.post('../files/insert.php',{"price":info.price}).success(function(data){
       if (data == true) {
       }
    });
}

现在,

$scope.addPrice= function(info){
    console.log(info.price); /* This will now show an array of values, 
                             that is value from each input within the ng-repeat*/
    $http.post('../files/insert.php',{"price":info.price}).success(function(data){
       if (data == true) {
       }
    });
}

在服务器端,您将在$data-&gt;price 中获得一组值。

【讨论】:

  • $data->price 是否从客户端获取价值?在服务器端检查 $data->price 值
  • 这是我在 php 文件中使用的内容 $price=mysqli_real_escape_string($con, $data->price); $query = "INSERT into myTbl (price) VALUES ('$price[0]')";我在表格中填写了所有价格元素!但它在数据库中插入 0。
  • 你可以在服务端回显$data->price,查看是否包含客户端传过来的值
  • 我还提交了其他输入,它们工作正常,但它们是单个值而不是数组,我真的不知道问题出在哪里!
猜你喜欢
  • 2016-06-12
  • 2018-11-09
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 2017-11-27
  • 1970-01-01
  • 2017-03-08
  • 2018-01-02
相关资源
最近更新 更多