0
I confess that I have been for a while in this small challenge, I am developing the cart part in my Ecommerce.
The goal is to display a message, the user click on "Add to cart" of a particular product.
The problem, is that I’m just getting to take and display the value of the first product that is displayed on my page (the other products, does not display message).
I am displaying the products through a foreach
, and put their ID inside a input
.
I think the problem lies in the way I’m calling mine function.
What could I be doing to solve this problem of mine?
PHP:
$connect = new PDO("mysql:host=localhost;dbname=ecommerce", "root", "");
$query = "SELECT * FROM produtos";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $cont){
?>
<h6><?=$cont['produto']?></h6> // Nome dos produtos
<input type="hidden" id="id" value="<?=$cont['ID_detalhes']?>"> // Estou buscando esse value...
<a href="#" id="adicionar_carrinho">+ Adicionar ao carrinho</a> // Botão que chama a função ajax
<?php }
AJAX:
<script>
$('#adicionar_carrinho').on('click', function(){
$.ajax({
url: 'verificar-carrinho.php',
data: {ID_detalhes: $('#id').val()},
method: 'POST',
success:function(data){
$('#msg_carrinho').html(data); // Aqui é exibe a mensagem na tela
}
})
});
</script>
check-cart.php:
if(isset($_POST['ID_detalhes'])){
echo "Produto (".$_POST['ID_detalhes'].") foi adicionado ao seu <b>carrinho</b>!";
}
I started working with AJAX
recently, I appreciate the help of all!
The id must be unique, so you can put it like this: <input type="Hidden" id="id-<?= $cont['ID']? >" value="<?= $cont['Id_details']? >">. You want when clicking on the product to show the ID?
– Rodrigo Zem
I got what I wanted, buddy, thanks
– Web Developer