I am trying to select an item from an array to push into the shopping cart via an ajax call but my JS skills are a little weak and need some pointers;
Heres ht js
$(document).on('click', 'button', function() {
var element = $(this).closest('.items');
var image = element.find('img')[0].src;
var name = element.find('input').val();
var price=document.find('input').val();
$.ajax({
type:'post',
url:'store_items.php',
data:{
item_src:img_src,
item_name:name,
item_price:price
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
}
function show_cart()
{
$.ajax({
type:'post',
url:'store_items.php',
data:{
showcart:"cart"
},
success:function(response) {
document.getElementById("mycart").innerHTML=response;
$("#mycart").slideToggle();
}
});
}
This is the while loop to spit out the list of products
while ($row = mysqli_fetch_array($r)) {
echo '
<div class="items" id="item">
<a href="doc_view.php?prod_id='.$row["prod_id"].'"> '.$row["prod_name"]. '</a>';
echo "
<div class='product_display'>
<p><img src='../admin/uploads/".$row['file_upload']."'></p>";
echo"
<input type='button' value='Add To CART' id='cart_button' onclick='cart(\"item\");' ";
echo'
<p>Price - '.$row["prod_price"]. '</p>
<input type="hidden" class="item_name" value="'.$row["prod_name"]. '">
<input type="hidden" class="item_price" value="'.$row["prod_price"]. '">';?>
<?php echo'
</div>
</div>
';
}
I am struggling to get the products to show in the cart - I display it like so:
$(document).ready(function(){
$.ajax({
type:'post',
url:'store_items.php',
data:{
total_cart_items:"totalitems"
},
success:function(response) {
document.getElementById("total_items").value=response;
}
});
});
<p id="cart_button" onclick="show_cart();">
<input type="button" id="total_items" value="">
</p>
<div id="mycart">
</div>
<a href="../index.php">home</a>
<div id="item_div">