I am able to load the cart in a popup window, however when I add something to a cart, I get the success popup window alerting me that something has been added but the cart always comes up empty. The session variables don't appear to be triggering. In the data post variables where I'm using jquery to add to cart in footer javascript, am I calling out the right atribute element in reference to var product_id = $(this).attr('id'); ?
ACTION.PHP - Session variables are created for cart
<?php session_start();
if(isset($_POST["action"]))
{
if($_POST["action"] == 'add')
{
if(isset($_SESSION["shopping_cart"]))
{
$is_available = 0;
foreach($_SESSION['shopping_cart'] as $keys => $values)
{
if($_SESSION['shopping_cart'][$keys]['product_id'] == $_POST['product_id'])
{
$is_available++;
$_SESSION['shopping_cart'][$keys]['product_quantity'] = $_SESSION['shopping_cart'][$keys]['product_quantity'] + $_POST['product_quantity'];
}
}
if($is_available == 0)
{
$item_array = array(
'product_id' => $_POST['product_id'],
'product_name' => $_POST['product_name'],
'product_price' => $_POST['product_price'],
'product_quantity' => $_POST['product_quantity']
);
$_SESSION['shopping_cart'][] = $item_array;
}
}
else
{
$item_array = array(
'product_id' => $_POST['product_id'],
'product_name' => $_POST['product_name'],
'product_price' => $_POST['product_price'],
'product_quantity' => $_POST['product_quantity']
);
$_SESSION['shopping_cart'][] = $item_array;
}
}
if($_POST['action'] == 'remove')
{
foreach($_SESSION['shopping_cart'] as $keys => $values)
{
if($values['product_id'] == $_POST['product_id'])
{
unset($_SESSION['shopping_cart'][$keys]);
}
}
}
if($_POST['action'] == 'empty')
{
unset($_SESSION['shopping_cart']);
}
}?>
ADD CART FORM AND FOOTER JAVASCRIPT - Index.php
<?php session_start();?>
<html>
<body>
echo '<input type="text" style="padding:5px;margin-left:6px;" size="2" name="quantity" id="quantity'.$a3["lessonid"].'" class="form-control" value="1" />
<input type="hidden" name="hidden_name" id="name'.$a3["lessonid"].'" value="'.$a3["title"].'" />
<input type="hidden" name="hidden_price" id="price'.$a3["lessonid"].'" value="'.$a3["price"].'" />
<input type="button" name="add_to_cart" style="padding: 5px; margin-top:5px;" class="add_to_cart" id="'.$a3["lessonid"].'" value="Add to Cart" /><br>';
</body>
</html>
<script>
$(document).ready(function(){
load_cart_data();
function load_cart_data()
{
$.ajax({
url:"fetch_cart.php",
method:"POST",
dataType:"json",
success:function(data)
{
$('#cart_details').html(data.cart_details);
$('.total_price').text(data.total_price);
$('.badge').text(data.total_item);
}
})
}
$(document).on('click', '.add_to_cart', function(){
var product_id = $(this).attr('id');
var product_name = $('#name'+product_id+'').val();
var product_price = $('#price'+product_id+'').val();
var product_quantity = $('#quantity'+product_id).val();
var action = 'add';
if(product_quantity > 0)
{
$.ajax({
url:"action.php",
method:"POST",
data:{product_id:product_id, product_name:product_name, product_price:product_price, product_quantity:product_quantity, action:action},
success:function(data)
{
load_cart_data();
alert("Item has been Added into Cart");
}
})
}
else
{
alert("Please Enter Number of Quantity");
}
});
$(document).on('click', '.delete', function(){
var product_id = $(this).attr('id');
var action = 'remove';
if(confirm("Are you sure you want to remove this product?"))
{
$.ajax({
url:"action.php",
method:"POST",
data:{product_id:product_id, action:action},
success:function(data)
{
load_cart_data();
alert("Item has been removed from Cart");
}
})
}
else
{
return false;
}
});
$(document).on('click', '#clear_cart', function(){
var action = 'empty';
$.ajax({
url:"action.php",
method:"POST",
data:{action:action},
success:function()
{
load_cart_data();
alert("Your Cart has been clear");
}
})
});
});
</script>