Deleted member 66701
Deleted member 66701
Hi all.
I'm pulling all products in a category back to display on a page (which is working fine). I have a javascript function attached to a "add to cart" to add each product to a $_SESSION cart. However, the problem is that it is only ever picking up the first product returned - i.e. no matter what "add to cart" link you click on, it's always adding the first product to the cart. Here's my code:-
Javascript:
HTML/PHP:
It's the alt part of $('.addtocartlink').attr('alt') that is always set to the first product id in the JS function. It should vary depending on the product id for each product returned.
Any ideas?
I'm pulling all products in a category back to display on a page (which is working fine). I have a javascript function attached to a "add to cart" to add each product to a $_SESSION cart. However, the problem is that it is only ever picking up the first product returned - i.e. no matter what "add to cart" link you click on, it's always adding the first product to the cart. Here's my code:-
Javascript:
Code:
<script>
$(document).ready(function() {
$('.addtocartlink').click(function() {
var randNum = Math.floor(Math.random() *10000000);
$.ajax({
url: "mng_cart.php?rand=" + randNum,
dataType: 'text',
type: 'POST',
data: 'action=add&product_id=' + $('.addtocartlink').attr('alt'),
beforeSend: function() {
$('#cartload').html('loading');
},
complete: function() {
$('#cartload').html('');
},
success:function(result) {
$("#holdcart").html('').append(result);
}
});
return false; //stops link clicking normally
});
});
HTML/PHP:
Code:
<?php while($row=mysqli_fetch_array($product)) {
?>
<p><br /><?php echo $row['p_short_desc']; ?></p>
<ul class="buttons">
<li class="cart"><a href="#" class="addtocartlink"
alt="<?php echo $row['product_id']; ?>">Add To Cart</a></li>
<li class="shop_btn"><a href="product.php?MainCat=<?php echo $_GET['MainCat'] ?>&SubCat=<?php echo $_GET['SubCat'] ?>&id=<?php echo $row['product_id']; ?>">Read More</a></li>
</ul>
<?php
} //end while loop ?>
It's the alt part of $('.addtocartlink').attr('alt') that is always set to the first product id in the JS function. It should vary depending on the product id for each product returned.
Any ideas?
Last edited by a moderator: