I have a "request" form that allows to insert many products via AngularJS (think of it as a ticket -> 1 number of request but many "lines" with the article + the amount requested).
Problem is that if I request 5 articles, sometimes it insert 2 of them in the DB, sometimes 3, sometimes even 1.
pedidosClienteNuevo.html
<div id="cabeza">
<h3>Nuevo Pedido</h3>
</div>
<div id="cuerpo_alta" ng-init="iniciarNuevoPedido()">
<form name="formPedidoClienteNuevo" role="form" novalidate>
<table class="tabla">
<tr>
<td>
<label>Numero de Pedido: </label>
</td>
<td>
<input type="text" name= "numeroPedido" placeholder="Numero de Pedido" value="{{nuevoNumeroPedido}}" />
</td>
</tr>
<tr>
<td>
<label>Codigo de Cliente: </label>
</td>
<td>
<input type="text" name= "idCliente" placeholder="Codigo de Cliente" ng-model="idCliente" required ng-class="{ 'ingreso-tiene-error' : formPedidoClienteNuevo.idCliente.$invalid && !formPedidoClienteNuevo.idCliente.$pristine, 'ingreso-es-valido' : !(formPedidoClienteNuevo.idCliente.$invalid && !formPedidoClienteNuevo.idCliente.$pristine) }"/>
</td>
<td>
<a ng-click="buscarCliente(idCliente)">Buscar</a>
</td>
</tr>
<tr>
<td>
</td>
<td>
<p ng-show="formPedidoClienteNuevo.idCliente.$invalid && !formPedidoClienteNuevo.idCliente.$pristine" class="help-block">Codigo de cliente requerida.</p>
<p ng-model="resultadoCliente">
{{resultadoCliente}}
</p>
</td>
</tr>
<tr>
<td>
<label>Codigo de Articulo: </label>
</td>
<td>
<input type="text" name= "idArticulo" placeholder="Codigo de Articulo" ng-model="idArticulo" required ng-class="{ 'ingreso-tiene-error' : formPedidoClienteNuevo.idArticulo.$invalid && !formPedidoClienteNuevo.idArticulo.$pristine,'ingreso-es-valido' : !(formPedidoClienteNuevo.idArticulo.$invalid && !formPedidoClienteNuevo.idArticulo.$pristine) }"/>
</td>
<td>
<a ng-click="buscarArticulo(idArticulo)">Buscar</a>
</td>
</tr>
<tr>
<td>
</td>
<td>
<p ng-show="formPedidoClienteNuevo.idArticulo.$invalid && !formPedidoClienteNuevo.idArticulo.$pristine" class="help-block">Codigo de articulo requerido.</p>
<p ng-model="resultadoArticulo">
{{resultadoArticulo}}
</p>
</td>
</tr>
<tr>
<td>
<label>Cantidad: </label>
</td>
<td>
<input type="text" name= "cantidad" placeholder="Cantidad" ng-model="cantidad" ng-class="{ 'ingreso-tiene-error' : formPedidoClienteNuevo.cantidad.$invalid && !formPedidoClienteNuevo.cantidad.$pristine, 'ingreso-es-valido' : !(formPedidoClienteNuevo.cantidad.$invalid && !formPedidoClienteNuevo.cantidad.$pristine) }"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<p ng-show="formPedidoClienteNuevo.cantidad.$invalid && !formPedidoClienteNuevo.cantidad.$pristine" class="help-block">Cantidad requerida.</p>
</td>
</tr>
<tr>
<td>
<label>Precio Minorista: </label>
</td>
<td>
<input type="text" name= "precioMinorista" placeholder="Precio Minorista" value= "{{precioMinoristaBuscado}}" disabled/>
</td>
</tr>
<tr>
<td>
<label>Fecha de Pedido: </label>
</td>
<td>
<input type="text" name= "fechaPedido" placeholder="Fecha de Pedido" value="{{datePedido | date:'dd/MM/yyyy'}}" disabled/>
</td>
</tr>
</table>
<label class="volverAtras">
<a href="pedidos_cliente.html#/lista">Atras</a>
</label>
<!-- Agrega el articulo actual a los detalles y permite la carga de un nuevo articulo-->
<button class="botonera" ng-click="confirmarArticulo(numeroPedido, idCliente, idArticulo, cantidad, precioMinoristaBuscado, fechaPedido)">Confirmar
</button>
<!-- Deshabilita los demas botones, excepto Guardar. Calcula el total del pedido-->
<button class="guardarBoton" ng-click="pedidosClienteInsert()" type="submit">GUARDAR
</button>
<table class="listadoDetalles tabla">
<caption class="tituloTablaDetalles">Detalles del pedido</caption>
<tr>
<th>Codigo Articulo
</th>
<th>Cantidad
</th>
<th>Precio
</th>
<th>Precio Final
</th>
<th>Borrar
</th>
</tr>
<tr ng-repeat="item in listadoActual">
<td>{{item.idArticulo}}
</td>
<td>{{item.cantidad}}
</td>
<td>{{item.precioMinorista}}
</td>
<td>{{item.precioFinalDeFila}}
</td>
<td>
<button ng-click="quitarElemento(item)">X
</button>
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
<td>TOTAL
</td>
<td>{{totalPedido}}
</td>
</tr>
</table>
</form> <!-- formPedidoClienteNuevo-->
</div>
pedidosClienteControlador.js (Controller, only the part of the function that send the data to PHP.
$scope.pedidosClienteInsert = function() {
//descomponer el array en tantas lineas como posea y mandar una solicitud a PHP por cada linea
angular.forEach($scope.listadoActual, function(valor,indice){
alert("Codigo del articulo:" + valor.idArticulo);
alert("Cantidad: " + valor.cantidad);
idCliente = valor.idCliente;
idArticulo = valor.idArticulo;
cantidad = valor.cantidad;
precioMinorista = valor.precioMinorista;
numeroPedido = $scope.nuevoNumeroPedido;
fechaPedido = $scope.fechaPedido;
$http.post("servidor/pedidosCliente/pedidosClienteInsert.php?numeroPedido="+numeroPedido+"&idCliente="+idCliente+"&idArticulo="+idArticulo+"&cantidad="+cantidad+"&precioMinorista="+precioMinorista+"&fechaPedido="+fechaPedido).success(function(data){
$scope.listadoActual = [];
$scope.totalPedido= 0.00;
alert("Exito");
});
});
pedidosClienteInsert.php (the one that conects to MySQL and attempt the download)
<?php
`require_once '../conectar2.php';
$numeroPedido = $_GET['numeroPedido'];
$idCliente = $_GET['idCliente'];
$idArticulo = $_GET['idArticulo'];
$cantidad = $_GET['cantidad'];
$precioMinorista = $_GET['precioMinorista'];
// convertir fechas al formato que tiene en la BD
$fecha1=$_GET['fechaPedido'];
list($dia, $mes, $anio)=explode("/", $fecha1);
$fechaPedido=$anio."-".$mes."-".$dia;
// Tomar el ID mas alto y sumarle 1
$result= $mysqli->query("SELECT MAX(idPedidoCliente) FROM pedidoCliente");
$resultFila = $result->fetch_row();
$idPedidoCliente=($resultFila[0]) + 1;
$query="INSERT INTO pedidoCliente VALUES ('$idPedidoCliente', '$numeroPedido', '$idCliente', '$idArticulo', '$cantidad', '$precioMinorista', '$fechaPedido')";
$result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$result = $mysqli->affected_rows;
echo $json_response = json_encode($result);
?>
Any thoughts? Sorry for the awful code.
Your XmlHttpRequest type is post
$http.post
But you are trying to get request params from GET var.In your php code try this
$rowData = json_decode(file_get_contents("php://input"));
And in you angular code change request like this.
$http({
url : "url",
data : {data Object}
}).success(function(response){
// handle response here
})