skip to Main Content

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


  1. Chosen as BEST ANSWER

    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()


  2. 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 .

    Login or Signup to reply.
  3. 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:

    1. Dump $_POST[‘confirmation’] to make sure it’s not actually empty

      echo '<pre>';
      echo die(var_dump($_POST['confirmation']));
      echo '</pre>';
      
    2. Try adding exit() at the end of the script

      if (isset($_POST['confirmation'])){
        clearCart(); 
        exit();
      }
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search