Skip to content Skip to sidebar Skip to footer

Jquery Delete Id From Element With Ajax Of A Database And Refresh

Hello at the moment i'm trying to use jQuery in order to get ids of multiple fields with ajax and send the data to remove it via php. So far i was able to delete the item but i can

Solution 1:

After looking into your code i have found few glitches:

  1. Element div of class list-items and span of class checkbox having same ids which is against the dom specification. (use prefix before ids like div-89, span-89 etc.).
  2. You are using the request type post but posting the data in url which is get method.
  3. You are sending request on click event of checkbox so it fires everytime when you select it so you cannot delete multiple items using this approach.

Answer : create a seperate button of delete and send request on click of that button.

Your code would be:

$('.list-item').click(function() {
    $(this).addClass('selected');
});

$(.delete).click(function() {
    var delIds = newArray();
    $('.selected').each(function() {
        delIds.push($(this).attr('id'));
    });

    $.ajax({
        url: 'something.php?action=delete&id='+delIds.join(),
        dataType: 'json',
        success: function(data) {
                    if(data.error === true)
                        {
                            $.errRorBar({ bdS: "error", html: data.message , delay: 5000 });
                        }
                        else
                        { 
                            $.errRorBar({ bdS: "success", html: data.message , delay: 5000});
                        }
                }, 
        error: function(XMLHttpRequest, textStatus, errorThrown)        {
                    $.errRorBar({ bdS: "error", html: "Opps! Something went wrong!" , delay: 5000 });

                    console.log(XMLHttpRequest);
                    console.log(textStatus);
                    console.log(errorThrown);

                    // console.log(XMLHttpRequest.responseText);
            }
    });
});

Hope this helps you

Post a Comment for "Jquery Delete Id From Element With Ajax Of A Database And Refresh"