Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created October 25, 2022 14:49
Embed
What would you like to do?
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nombres = $_POST["nombres"];
$marcas = $_POST["marcas"];
$precios = $_POST["precios"];
$subtotal = 0;
for ($i = 0; $i < count($nombres); $i++) {
$nombre = $nombres[$i];
$marca = $marcas[$i];
$precio = floatval($precios[$i]);
$subtotal += $precio;
printf("%s ($%s) marca %s<br>", $nombre, number_format($precio, 2), $marca);
}
$porcentajeIva = 0.16;
$iva = $subtotal * $porcentajeIva;
$total = $subtotal + $iva;
echo "<br><strong>Subtotal: </strong>$" . number_format($subtotal, 2) . "<br>";
echo "<strong>IVA: </strong>$" . number_format($iva, 2) . "<br>";
echo "<strong>TOTAL: </strong>$" . number_format($total, 2) . "<br>";
echo "<br><a href=''>Volver al formulario</a>";
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Productos</title>
</head>
<body>
<form action="" method="post">
<?php
$cantidadProductos = 5;
?>
<?php for ($i = 0; $i < 5; $i++) { ?>
<p>Producto <?php echo $i + 1 ?></p>
<label for="">Nombre: </label>
<input type="text" name="nombres[]" required>
<label for="">Marca:</label>
<select name="marcas[]" required>
<option selected value="Marca 1">Marca 1</option>
<option value="Marca 2">Marca 2</option>
<option value="Marca 3">Marca 3</option>
</select>
<label for="">Precio:</label>
<input type="number" name="precios[]" required>
<br>
<?php } ?>
<br>
<input type="submit">
<input type="reset">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment