Last active
April 18, 2018 10:54
-
-
Save ojgarciab/05ad1dbb7ff371c1ba3bb70f9325cba7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$carrito7 = [ | |
[ | |
0, | |
'Foto1', | |
150, | |
1, | |
150, | |
], | |
[ | |
1, | |
'Foto2', | |
200, | |
2, | |
400, | |
], | |
[ | |
2, | |
'Foto3', | |
300, | |
3, | |
900, | |
], | |
]; | |
?><!DOCTYPE html> | |
<html lang="es"> | |
<head> | |
<title>Título de ejemplo</title> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | |
<!-- Bootstrap --> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous" /> | |
</head> | |
<body style="padding-top: 2rem;"> | |
<div class="container"> | |
<form action="eliminarFotografia.php" method="post" name="formulario"> | |
<input type="hidden" id="carrito7" value="<?= htmlspecialchars(serialize($carrito7)) ?>" /> | |
<table> | |
<tr> | |
<th>NombreFoto</th> | |
<th>Precio</th> | |
<th>Cantidad</th> | |
<th>Subtotal</th> | |
</tr> | |
<?php | |
//Muestro una tabla con todos los productos que se añadieron al carrito | |
$numProductos = count($carrito7); | |
$total = 0; | |
for($i = 0; $i < $numProductos; $i++){ | |
// Calculo el precio total de la compra | |
$total += $carrito7[$i][4]; //precio total | |
?> <tr> | |
<td><?= htmlspecialchars($carrito7[$i][1])?></td> | |
<td><?= htmlspecialchars($carrito7[$i][2])?></td> | |
<td><input type="number" name="cantidad" | |
value="<?= htmlspecialchars($carrito7[$i][3]) ?>." min="0"></td> | |
<td><?= htmlspecialchars($carrito7[$i][4])?></td> | |
<td><input type='button' name="eliminar" | |
data-id="<?= htmlspecialchars($carrito7[$i][0]) ?>" | |
value="Eliminar" onclick="eliminarFoto(event)"></td> | |
</tr> | |
<?php | |
} | |
?> <tr> | |
<th colspan="2">Total:</th> | |
<td><?= $total ?></td> | |
</tr> | |
</table> | |
<input type='hidden' name='total' value="<?= htmlspecialchars($total) ?>" /> | |
<input type='submit' name='comprar' value='Comprar'> | |
<div id='mensaje'></div> | |
</form> | |
</div> | |
<!-- jQuery --> | |
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script> | |
<!-- Bootstrap --> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/js/bootstrap.min.js" integrity="sha384-h0AbiXch4ZDo7tp9hKZ4TsHbi047NrKGLO3SEJAg45jXxnGIfYzk4Si90RDIqNm1" crossorigin="anonymous"></script> | |
<script> | |
//Elimino producto del carrito | |
function eliminarFoto(event){ | |
/* Enviamos en "data" los datos de la siguiente manera */ | |
jQuery.ajax({ | |
url: "eliminarFotografia.php", | |
/* Manera más elegante de enviar los datos: */ | |
data: { | |
id: event.target.dataset.id, | |
carrito7: $('#carrito7').val(), | |
}, | |
type: "POST", | |
success: function (data) { | |
$('#mensaje').html(data); | |
}, | |
error: function () {} | |
}); | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<pre style="font-size: 70%;"><?php | |
/* Convertimos en entero ya que no podemos eliminar un índice numérico con una cadena */ | |
$id = intval($_POST['id']); | |
$carritoDes = unserialize($_POST['carrito7']); //deserializo la variable carrito7 y lo guardo en la variable carritoDes | |
/* Mostramos, para depurar, que todo ha llegado bien */ | |
var_export($carritoDes); | |
//Recorro carritoDes para eliminar el producto | |
foreach ($carritoDes as $i => $datos) { | |
if($datos[0] == $id) { | |
/* Debemos quitar el elemento de los datos deserializados */ | |
unset($carritoDes[$i]); | |
} | |
} | |
/* Mostramos, para depurar, que se ha borrado el elemento deseado */ | |
var_export($carritoDes); | |
echo ""; | |
?></pre> | |
<p>ID recibido: <?= $id ?></p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment