Hi, I know it's a very simple thing. In fact, I tried to solve this on the client's side, but I decided to try to do this in the database directly in order to continue with my project, I would appreciate corrected my code. The problem is with the field labeled, it is a checkbox that is stored in an array at this height I do not care where I just want to solve my problem either on the client or on the server.
CREATE TABLE capturar_pedido (
cliente varchar(255) NOT NULL,
orden_de_compra varchar(255),
producto varchar(255) NOT NULL,
unidad varchar(255) NOT NULL,
cantidad double NOT NULL,
fecha_de_embarque Date NOT NULL,
notas varchar(255),
etiquetado char(2) default 'n',
id_pedido BIGINT(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (id_pedido)
)
I also leave the form
Start
<br/><br/>
<form action="agregarpedidos.php" method="post" name="form1">
<div class="container1">
<div align="left" class="espacio10">
<strong>Cliente:</strong>
<select required name="cliente">
<?php
echo '<option value="">Selecciona un cliente</option>';
$sql = "Select cliente from clientes";
$query = $db->prepare($sql);
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['cliente'].'</option>';
}
?>
</select>
<strong>Orden de compra</strong>
<input type="text" name="orden_de_compra" placeholder="orden de compra" required="required" maxlength="255"/>
<button class="add_form_field" id="botonagregarproducto" >Agregar producto <span style="font-size:16px; font-weight:bold;">+ </span></button>
</div>
<table class="table-bordered table-striped">
<tr>
<th>PRODUCTO</th>
<th>UNIDAD</th>
<th>CANTIDAD</th>
<th>FECHA DE EMBARQUE</th>
<th>NOTAS</th>
<th>ETIQUETADO</th>
<th>Opciones</th>
</tr>
</table>
</div>
<script>
$(document).ready(function() {
var max_fields = 10;
var wrapper = $(".container1");
var add_button = $(".add_form_field");
var x = 1;
$(add_button).click(function(e){
e.preventDefault();
if(x < max_fields){
x++;
var selectproductos = "<?php $sql = "Select producto from productos"; $query = $db->prepare($sql);
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['producto'].'</option>';
}
?>";
var selectunidades = "<?php $sql = "Select unidad from unidades";
$query = $db->prepare($sql);
$query->execute();
while($row = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option>'.$row['unidad'].'</option>';
}
?> ";
$(wrapper).children('table').append('<tr> <td> <select required name="productos[]"><option value="">Selecciona un Producto</option>'+selectproductos+'</select><td><select required name ="unidad[]"><option value="">Selecciona una unidad</option>'+selectunidades+'</select></td><td><input type="text" class="inputancho" name="cantidad[]" placeholder="cantidad" required="required"/></td><td><input type="date" name="fecha_de_embarque[]" required="required"/></td> <td> <textarea rows="2" cols="30" name="notas[]" id="notas" maxlength="255"></textarea> </td> <td><input type="checkbox" name="etiquetado[]" value="Si"> </td><td><a href="#" class="delete">Eliminar</a></<td></tr>'); //add input box
}
else
{
alert('You Reached the limits')
}
});
$(wrapper).on("click",".delete", function(e){
e.preventDefault(); $(this).parent('td').parent('tr').remove(); x--;
})
});
</script>
<input type="submit" name="Submit" onclick="GetTextValue()" id="submit-pedidos" value="Enviar" class="bt"/>
</form>
And the code in php
<body>
<?php
//including the database connection file
include_once("resource/Database.php");
if(isset($_POST['Submit'])) {
$cliente = $_POST['cliente'];
$orden_de_compra = $_POST['orden_de_compra'];
$productos= (is_array($_POST['productos'])) ? $_POST['productos'] : array();
$unidad= (is_array($_POST['unidad'])) ? $_POST['unidad'] : array();
$cantidad= (is_array($_POST['cantidad'])) ? $_POST['cantidad'] : array();
$fecha_de_embarque= (is_array($_POST['fecha_de_embarque'])) ? $_POST['fecha_de_embarque'] : array();
$notas = (is_array($_POST['notas'])) ? $_POST['notas'] : array();
$etiquetado= (is_array($_POST['etiquetado'])) ? $_POST['etiquetado'] : array();
for ($i = 0; $i < count($etiquetado); $i++)
{
if ($etiquetado[$i] == null) $etiquetado[$i] = 'No';
}
if(empty($cliente) || empty($orden_de_compra) || empty($productos) ||empty($unidad) ||empty($cantidad) || empty($fecha_de_embarque) ) {
if(empty($cliente)) {
echo "<font color='red'>Campo cliente esta vacio.</font><br/>";
}
if(empty($orden_de_compra)) {
echo "<font color='red'>orden de compra esta vacio.</font><br/>";
}
if(empty($productos)) {
echo "<font color='red'>Campo producto esta vacio.</font><br/>";
}
if(empty($unidad)) {
echo "<font color='red'>Campo unidad esta vacio.</font><br/>";
}
if(empty($cantidad)) {
echo "<font color='red'>Campo cantidad esta vacio.</font><br/>";
}
if(empty($fecha_de_embarque)) {
echo "<font color='red'>Fecha de embarque esta vacio.</font><br/>";
}
//link to the previous page
echo "<br/><a href='javascript:self.history.back();'>Regresa</a>";
} else {
// if all the fields are filled (not empty)
$insertStmt = $db->prepare("INSERT INTO capturar_pedido(cliente, orden_de_compra, producto, unidad, cantidad , fecha_de_embarque, notas, etiquetado) VALUES(:cliente, :orden_de_compra, :producto, :unidad, :cantidad , :fecha_de_embarque, :notas, :etiquetado)");
$i=0;
foreach ($productos as $producto) {
$insertStmt->execute(array('cliente' => $cliente,'orden_de_compra' => $orden_de_compra,'producto' => $producto,'unidad' => $unidad[$i],'cantidad' => $cantidad[$i] ,'fecha_de_embarque' => $fecha_de_embarque[$i] ,'notas' => $notas[$i] ,'etiquetado' => $etiquetado[$i] ));
$i++;
}
// Alternative to above bindparam and execute
// $query->execute(array(':name' => $name, ':email' => $email, ':age' => $age));
//display success message
header("Location: index.php");
}
}
?>
</body>