I have shoppingbasket.php which has a HTML form, and is linked to incrementing.js I have plus and minus buttons which update the quantity of the item in the basket, however I am having issues updating my database (in PHPMyAdmin) so that the 'basket' table updates automatically when someone uses the plus or minus buttons.
Shoppingbasket.php includes:
Print '<div id="list_item">
<img src="Images/' . $img . '">
<div id="rightside">
<a href="singlepage.php?subject=' . $product_id . '">' . $name . '</a><br>
<p>' . $subtitle . '</p><br>
<form id="quantity" method="POST" action="">
<div class="numbers-row">
<input type = "text" id="' . $product_id . '" value="' . $quantity . '">
</div>
</form>
</div>
</div>';
Incrementing.js includes:
$(function() {
$(".numbers-row").append('<div class="inc button">+</div><div class="dec button">-</div>');
$(".button").on("click", function() {
var $button = $(this);
var oldValue = $button.parent().find("input").val();
if ($button.text() == "+") {
var quantity = parseFloat(oldValue) + 1;
} else {
// Don't allow decrementing below zero
if (oldValue > 0) {
var quantity = parseFloat(oldValue) - 1;
} else {
quantity = 0;
}
}
});
var pid = $button.attr("id");
$.ajax({
type: "POST",
url: "basket2table.php",
data: { pid : pid, quantity: quantity, },
success: function() {
$button.parent().find("input").val(quantity);
}
});
});
Basket2table.php includes:
<?php include("dbconnect.php");
if(isset($_POST['quantity'])) {
$quantity = $_POST['quantity'];
}
if(isset($_POST['pid'])){
$pid = $_POST['pid'];
}
$sql_query = "UPDATE Basket
SET quantity='$quantity'
WHERE product_ID='$pid' ";
$result = $db-> query($sql_query);
?>