How to add to cart without reload page using php code

we are add to cart in e-commerce website without reload page use the php simple code. and get id the cart in session that are the send value through cart button with session.

//Get session value
$_SESSION['cart'];

if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}
else{
//Get product id
$sql_p="SELECT * FROM products WHERE id={$id}";
$query_p=mysqli_query($con,$sql_p);
if(mysqli_num_rows($query_p)!=0){
$row_p=mysqli_fetch_array($query_p);
$_SESSION['cart'][$row_p['id']]=array("quantity" =>1, "price" => $row_p['product_price']);
header('location:my-cart.php');
}else{
$message="Product ID is invalid";
}
}
}
// Html code add to cart

<input class="input-text qty text" type="number" size="4" title="Qty" value="1" name="qty" min="1" step="1" max="5" />
<a href="#emptycart" class="btn btn-primary btn-lg" />add to cart</a>

Social Share

Leave a Comment