James Allan
asked on
Adding checkmark icon to product added in cart using PHP/Jquery
I am trying to add a checkmark to the products that have been added to cart. I am using ajax to return. But i know i am doing wrong. I got a bit lost. I made with php first,and it works,but only displays when i refresh the page. My php code...
I want to use ajax to display the checkmark.
This is where i get my product id and store them to session. My add to cart function and item counter code..(cart_function.php)
And this is the script in my gallery...
i am trying to display in here
if(isset($_SESSION['product'][$id])){
$added='<a href="" class="badge-corner" product_id="'.$id.'" >
<span class="fa fa-check"></span></a>';
}else{
$added='';
}
I want to use ajax to display the checkmark.
This is where i get my product id and store them to session. My add to cart function and item counter code..(cart_function.php)
<?php header('Content-Type: application/json');
session_start();
if (isset($_GET['action'])) {
$action = $_GET['action'];
$prod = $_GET['prod_id'];
$result="";
switch ($action) {
case 'add':
$result = add_prod($prod);
break;
default:
$result = ['result'=>'error'];
break;
}
}
// Calculate subtotal here
$result['totals'] = new stdClass;
$total_in_cart=count($_SESSION['product']);
$_SESSION['products']=$total_in_cart;
if(isset($_SESSION['product'][$prod])){
$added='<a href="" class="badge-corner" product_id="'.$prod.'" ><span class="fa fa-check"></span></a>';
}else{
$added='';
}
$result['added'] = $added;
$result['items'] = $total_in_cart;
echo json_encode($result);
function add_prod($prod){
//add function
$_SESSION['product'][$prod] = 1;
$_SESSION['products']++;
return ['result'=>'success'];
}
?>
And this is the script in my gallery...
$('.actions').click(function(e){
e.preventDefault(e);
var action = $(this).attr('data-action'); //gets button id
var id = $(this).attr('product_id');
console.log("triggered action " + action + " for product " + id);
$.ajax({
url: 'cart_functions2.php',
type : 'GET',
data: {action:action,prod_id:id},
dataType: 'json'
}).done(function(data) {
console.log("ajax call returned the following: " + JSON.stringify(data));
if (data.result == "success") {
$("#items").html(data.items);
$(".show").html(data.added) + id;
//some debugging script
i am trying to display in here
<div class="show">'.$added.'</div>
When i click add product, all my products gets checked at once. Why? Please help me.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You're welcome!
ASKER
You guys are the best o/