I am attemtping to use Ajax and PHP to create a shopping cart. At the moment I console.log and can see that the correct product ID is being selected when I click 'add cart' but nothing happens in my PHP when I try and var dump the POST data that should be sent. I need to be able to get that product ID in my PHP, search the database for the product and then add to the basket.
PHP
<?php
session_start();
include('classes/class-database-functions.php');
$database = new Database_Functions();
if($_POST) {
var_dump($_POST);
}
?>
JS
$(document).ready(function(){
$(".form-item").submit(function(e){
var form_data = $(this).serialize();
var button_content = $(this).find('button[type=submit]');
button_content.html('Adding...'); //Loading button text
$.ajax({ //make ajax request to cart_process.php
url: "cart_process.php",
type: "POST",
dataType:"json", //expect json value from server
data: form_data
}).done(function(data){ //on Ajax success
$("#cart-info").html(data.items); //total items in cart-info element
button_content.html('Add to Cart'); //reset button text to original text
alert("Item added to Cart!"); //alert user
if($(".shopping-cart-box").css("display") == "block"){ //if cart box is still visible
$(".cart-box").trigger( "click" ); //trigger click to update the cart box.
}
})
e.preventDefault();
});
});