$(document).ready(function() {
$(".eliminar").click(function(e) {
e.preventDefault();
var id = $(this).attr('data-id');
//alert(id);
$(this).closest('.holder-cesta').remove();
//Esta parte no me funciona.
$.post('./php/carro_compra/eliminar.php', {
Id: id
}, function(a) {
if (a == '0') {
location.href = "./cesta";
}
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="holder-cesta">
<h4>Product 1</h4>
<button class="eliminar" data-id="1">Delete</button>
</div>
<div class="holder-cesta">
<h4>Product 3</h4>
<button class="eliminar" data-id="3">Delete</button>
</div>
<div class="holder-cesta">
<h4>Product 2</h4>
<button class="eliminar" data-id="2">Delete</button>
</div>
I am creating the boton
of removing the products from the basket. Each added product will have a boton
to eliminate.
The results of the shopping cart I keep in a session called $_SESSION['carrito'];
Here goes the code where I call the products and their delete button:
<?php
//Array carrito de compras.
$data = $_SESSION['carrito'];
$total = 0;
for ($i=0; $i<count($data); $i++) {
?>
<div class="holder-cesta">
<h4><?php echo $data[$i]['Titulo']; ?></h4>
<button class="eliminar" data-id="<?php echo $data[$i]['Id']; ?>">Eliminar</button>
</div>
<?php
}
?>
data-id
I get the id
product.
Let's look at the jQuery code:
$(document).ready(function() {
$(".eliminar").click(function(e) {
e.preventDefault();
//Obtengo el id desde nuestro boton.
var id = $(this).attr('data-id');
//Mi alert lanza correctamente el id associado.
//alert(id);
$(this).closest('.holder-cesta').remove();//Remueve correctamente
//AJAX
//Aqui viene mi problema,no obtengo var id en $.post y tampoco parece que vaya a la url eliminar.php
$.post('./php/carro_compra/eliminar.php', {
Id:id
},function(a) {
if (a=='0') {
location.href="./cesta";
}
});
});
});
If I do a var_dump($_POST['Id']);
in my file delete.php, I really do not know anything about that function, if I modify a bit the ajax, it sends me a NULL.
The jQuery
works fine, since it correctly launches the alert and also eliminates the div.holder-basket, but the problem is as if it never goes to the url of $.post
where I want to actuailzar the array through the Id
as follows:
if ($arreglo[$i]['Id']!= $_POST['Id']) {
}
I also attach the file delete.php with which I want to update my shopping cart when I delete a product:
<?php
//Session start
session_start();
//Get shoping cart data.
$arreglo = $_SESSION['carrito'];
for ($i=0; $i<count($arreglo); $i++) {
//Logica, si id del articulo es diferente a la variable obtenido por Ajax.
if ($arreglo[$i]['Id']!= $_POST['Id']) {
$datosnuevos[] = ['Id' => $arreglo[$i]['Id'], 'Titulo' =>$arreglo[$i]['Titulo'], 'Precio' => $arreglo[$i]['Precio'], 'Icon' => $arreglo[$i]['Icon'], 'Cantidad' => $arreglo[$i]['Cantidad'] ];
}
}
if (isset($datosnuevos)) {
//Modifico la sesion, dejando el resto de articulos comprados.
$_SESSION['carrito'] = $datosnuevos;
//Modificamos el valor del carro.
$data = $_SESSION['carrito'];
$value_carrito = count($data);
$_SESSION['compras'] = $value_carrito;
//Asi tambien se recarga la página con los datos actualizado
echo "0";
} else {
unset($_SESSION['carrito']);
unset($_SESSION['compras']);
echo "0";
}
?>