Amit Bhatnagar
Amit Bhatnagar

Reputation: 1

Unable to delete more than one cart items

I have developed a shopping website where i am using ajax to process add to cart and remove from cart. Add to cart is working fine but remove from cart is creating an strange issue. I am able to delete only one record from it and if i want to delete more i need to manually refresh the page and then i would be able to delete only one more. This is so strange and i am unable to find where the bug is.

here is the code :

jQuery('.remove').click(function(data) {

    var pi = jQuery(this).attr('name');
        var y = jQuery(this).attr('name');

    $.ajax({
                url: "delcart.php?pid="+pi+"&data="+y,
                type: 'GET',
                success: function(s){
                    var $container = $("#content");
      //  $container.refresh("index.php");
$("#content").load(location.href + " #content");
$("#success2").show().fadeOut(6000);
                },
                error: function(e){
                    alert('Error Processing your Request!!');
                }
            });

});

HTML Button.

<button name="<?php echo $ca['ID'];?>" class="remove close_product color_dark tr_hover">x</button>

this button is into the while loop and each time name property consisting the unique ID of database cart table.

here is the delcart.php :

<?php
include_once "config.php";
$id = $_GET['pid'];
$ip = $_SERVER['REMOTE_ADDR'];

$de = mysqli_query($con,"DELETE from cart where ID='{$id}' and ip='{$ip}'");
?>

can anyone please figure what have i missed or where the bug is. This would be a huge help. Thanks in advance.

Upvotes: 0

Views: 55

Answers (1)

flynorc
flynorc

Reputation: 829

Not sure if I have the solution for you but I guess it's worth a shot. My guess is that your buttons with class remove are located somewhere in div with id of container, correct?

if so, then you "lose" the .click handler when you call the .load function that replaces the content of #container div.

the solution would (could) be to change the .click listener to sth like

$('#some-div-container-that-stays').on("click", ".remove", function(data) {

Or at least try to put an console.log() into the .click action to see if it triggers or not

Upvotes: 1

Related Questions