Array solo recoge el último valor seleccionado
Publicado por Augusto (11 intervenciones) el 14/10/2020 07:35:04
hola a todos buenos dias/tardes/noche
en el siguiente pedido de ayuda
se intenta armar un carrito de compras con sesiones
por medio de formulario + una función

problema
si selecciono todos retorna unicamente el ultimo array
entonces si selecciono el ultimo array si devuelve la seleccion

pero en cualquier otro caso de seleccion retorna vacio

abajo dejo el codigo
Productos.php
agregarProducto.php
Funciones.php
en el siguiente pedido de ayuda
se intenta armar un carrito de compras con sesiones
por medio de formulario + una función

problema
si selecciono todos retorna unicamente el ultimo array
entonces si selecciono el ultimo array si devuelve la seleccion

pero en cualquier otro caso de seleccion retorna vacio

abajo dejo el codigo
Productos.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php include 'index.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="estilos.css">
<title>Productos</title>
</head>
<body>
<h2>Productos</h2>
<?php
$MostrarProd="SELECT * FROM productos";
if($resultado=$con->query($MostrarProd)) {
while ($fila = mysqli_fetch_assoc($resultado)) {
?>
<table class="tabla-prod"> </tr>
<form method="POST" action="agregarProducto.php">
<td> <?php echo "<input type=hidden name=prod value=$fila[numProductos]>"." "."</input>"; ?> </td>
<td> <?php echo "<input type=hidden name=nombre value=$fila[nombre]>"."$fila[nombre]"."</input>"; ?> </td>
<td> <?php echo "<input type=hidden name=precio value=$fila[precio]> "."$fila[precio]"."</input>"; ?> </td>
<td> <?php echo "<input type=hidden name=cantidad value=$fila[cantidad]>"."$fila[cantidad]"."</input>"; ?> </td>
<td><input type="number" placeholder="Cantidad" name="Cantidadselec"></td>
<td> <input type="submit" value="Agregar Al Carrito" name="agregar"></td><tr>
<?php }} ?>
</form>
</table>
<p> <?php ?></p>
</body>
</html>
agregarProducto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
include 'Funciones.php';
if (isset($_POST["agregar"])){
session_start();
$numProductos=$_POST['prod'];
$nombre=$_POST['nombre'];
$precio=$_POST['precio'];
$cantidad=$_POST['cantidad'];
$Cantidadselec=$_POST['Cantidadselec'];
if (empty($numProductos) || empty($nombre) || empty($precio) ||
empty($cantidad) || empty($Cantidadselec) ) {
echo "están vacios";
}else {ListaProducto($numProductos,$nombre,$precio,$Cantidadselec,$con);
var_dump($_SESSION['Carrito']);
}
}
?>
Funciones.php
1
2
3
4
5
6
7
8
9
10
11
function ListaProducto($numProductos,$nombre,$precio,$Cantidadselec,$con){
$Carrito = array(
"numProductos"=>$numProductos,
"nombre" =>$nombre,
"precio" =>$precio,
"Cantidadselec" =>$Cantidadselec,
);
$_SESSION['Carrito']=$Carrito;
return $_SESSION['Carrito'];
}
Valora esta pregunta


0