【问题标题】:No POST data after dynamically creating a form and submitting it动态创建表单并提交后没有POST数据
【发布时间】:2015-11-12 14:02:11
【问题描述】:

我正在使用 AngularJSPHP 为自己创建一个小商店系统(学习目的),但遇到以下问题:

我创建了一个动态购物车,并在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&ouml;schen</button></p>
		    </form>

【问题讨论】:

    标签: php angularjs forms


    【解决方案1】:

    &lt;form&gt; 元素不能是&lt;tbody&gt; 元素的子节点。

    您可以在表单中包含整个表格。您可以在表格单元格中拥有整个表单。

    任何其他都会导致浏览器错误恢复,从而将表单转储到表格之外而留下输入。

    使用basic QA。编写有效的 HTML。

    【讨论】:

    • 谢谢,不知道。将学习适当的结构。
    猜你喜欢
    • 2011-12-21
    • 2015-03-11
    • 2017-01-21
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多