This is the script that will post the result to the clearCartFunction.php page
<script type="text/javascript">
$(document).ready(function(){
$("#deleteButton").click(function(){
var result = confirm('Are you sure?');
if(result){
$.post("clearCartFunction.php",{confirmation: result},
function(status){
alert(status);
});
}
});
});
</script>
There is a button on my website
<button type="submit" name="clearCart" value="clear" id="deleteButton">Clear Cart</button>
When I click on this button it should execute the function but I can’t seem to get it to work.
<?php
session_start();
function clearCart(){
$_SESSION['finalCart']=array();
$_SESSION['cart']=array();
}
if (isset($_POST['confirmation'])){
clearCart();
}
?>
3
Answers
Oh okay I realized what was the issue. Basically the code was working but since cart is an array, I needed to refresh the page for it to happen. So I just had to add a location.reload()
use postman software and test that php file by sending it request .see whether it works itself or not.maybe that JQ function is not flawless .
Since you didn’t specify what exactly is wrong with your code I’m going to take a guess and provide you with two options:
Dump $_POST[‘confirmation’] to make sure it’s not actually empty
Try adding exit() at the end of the script