我正在使用AngularJS和PHP为自己创建一个小商店系统(学习目的),但我遇到了以下问题:
我创建了一个动态购物车,并在ng-click
上填充了列表中单击对象的一些信息。我保存到一个名为scope.cart
的范围变量中,然后由购物车表中的ng-repeat
显示。然后,我还自动创建了隐藏的输入字段,以便在提交后将它们传递给$_POST
变量。因为我的POST在提交后是空的,所以在发送公式时,我尝试使用ng-submit
指令从scope.cart
数组中创建一个json文件,这样我就可以通过PHP调用它了。但这也不起作用。现在我被卡住了,不知道问题可能是什么。
然而,我认为输入字段是空的,即使浏览器在源代码中动态添加了新字段,当我点击提交时,就会出现空购物车的状态。
解决我的问题并将AngJS数据发送到服务器的最佳方法是什么?
navApp.controller('MainCtrl', ['$scope', '$http', function (scope, http){
scope.submit = function() {
var time = Math.floor(Date.now() / 1000);
http.post('../dampfen/cfg/orders/cart_' + time + '.json', scope.cart).then(function(data) {
alert("Order successfully transferred!");
});
}
}]);
<div class="panel-body">
<table class="table table-hover">
<thead><tr><th colspan="4">Bestellung</th></thead><tbody>
<form name="orderForm" method="POST" ng-submit="submit()" action="index.php?checkout">
<tr ng-repeat="item in cart">
<input type="hidden" name="order[]" value="{{item.id}}" />
<td>1x</td><td><strong>{{item.name}}</strong></td><td>{{item.preis}} <span class="glyphicon glyphicon-euro"></span></td><td><button class="btn btn-xs btn-danger" ng-click="removeFromCart($index)"><span class="glyphicon glyphicon-remove"></span></button></td></tr>
</tbody>
</table>
</div>
<div class="panel-footer">
<p class="text-right"><strong>{{cart.sum}} <span class="glyphicon glyphicon-euro"></span></strong></p>
<p class="text-left"><button type="submit" class="btn btn-sm btn-success">Checkout</button> <button type="button" class="btn btn-sm btn-danger" ng-click="deleteCart()">Warenkorb löschen</button></p>
</form>
<form>
元素不能是<tbody>
元素的子节点。
你可以在一个窗体中有一整张表。你可以在单元格中有一整个窗体。
任何其他操作都会导致浏览器错误恢复,这可能会将表单转储到表外,留下输入。
使用基本的QA。编写有效的HTML。