0

I'm trying to remove a product from cart using php. I'm able to delete the product using input and button type submit like this.

<input type="checkbox" name="remove_code[]" value="<?php echo htmlentities($row['id']);?>" />
<button id="remove-from-cart" type="submit">delete</button>

I have to select each checkbox and click submit to remove product everytime. Now I want to remove item from cart using ajax without refreshing the whole page and I want to use only button or img tag instead of input tag. This is my php code and

// Code for Remove a Product from Cart
if(isset($_POST['remove_code']))
 {

  if(!empty($_SESSION['cart'])){
    foreach($_POST['remove_code'] as $key){
        
            unset($_SESSION['cart'][$key]);
    }
        // echo "<script>alert('Your Cart has been Updated');</script>";
   }
 }

this is my ajax request

$(document).on("click", "#remove-from-cart", function () {
        
        $.ajax({
            url: "my-cart.php",
            type: "post",
            success: function () {
                
                
            },
        });
   });
10
  • What's the problem? Commented Aug 27, 2020 at 7:37
  • Hi Islam!! I want to remove product from cart using ajax without refreshing the page. Now I'm struggling to find which php value that I've to use in ajax response to delete item. Commented Aug 27, 2020 at 7:53
  • How are you sending your data to your ajax? Commented Aug 27, 2020 at 7:54
  • using my-cart.php file. The php code mentioned above is in my-cart.php file. Commented Aug 27, 2020 at 7:57
  • How are you sending your data from your code to your file? I think you'll need to read about ajax forms if you can't understand my question. Commented Aug 27, 2020 at 7:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.