I am trying to send data from modal with ajax This is how i am trying to process it, but it doesnt do anything, if i comment out the first IF block then i can get to $_POST['id'] variable, otherwise it doesnt work.
if(isset($_REQUEST['id']) && isset($_SESSION['user'])){
$sql = "INSERT INTO cart (user_id, id_p, quantity) VALUES(?, ?, ?)";
$stmt = $db->prepare($sql);
$stmt->bind_param('sss', $_SESSION['user'], $_REQUEST['id'], $_REQUEST['quantity'] );
$stmt->execute();
$stmt->close();
}
/*
@@@@
-process product ID that we send from modal via AJAX
-store it in 2D array
@@@@
*/
if(!isset($_SESSION['cart'])){
$_SESSION['cart'] = array();
}
if(isset($_POST['id'])){
$_SESSION['cart'][] = $_POST['id'];
}
?>
Here is how i am sending data with ajax, i can see them both with inspect in header but i cant seem to access them.
$(document).on('click','.add-to-basket',function(){
var productID = $('#itemBox').data('id');//Variable for product ID
var productQ = document.getElementById("quantity").value;//Variable for product Q
// skryty input do ktere si zapsal ID produktu po otevreni modalu
$.ajax({
type: 'POST',
cache: false,
data: {id : productID, quantity: productQ},
url: 'cart/includes/cartid.php', // tomuto souboru predas idecko produktu, zapises do kosiku atd.
success: function(data) {
window.location.replace("/shop/cart/cart.php")
alert("Product was added to basket");
}
});
});