1

So I am working on a registration system for the place I worked at this summer. I have a system where they can "add" things to a basket, but I figured that I also will need to give them the option to delete those items if they so wish. My question comes in here, is there a way to delete a record from the database and update the page in real time? I am using PHP, MySQL, and jQuery.

4 Answers 4

1

Adarsh is correct, use an Ajax post:

basic example of your cart page off the top of my head using jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>    
<script language="javascript">
    $(document).ready(function(){
        $(".deleteItem").click(function());
        var dataString = "deleteID="+$(this);
        $.ajax({
            type: "POST",
            dataType: "HTML",
            url: "deleteScript.php",
            data: dataString,
            success: function(data) {
                $("#LI"+$(this)).remove();
            }
        });
    });
    </script>

    <ul>
        <li id="LI1">
            <input type="button" class="deleteItem" id="1"> 1st Item in your cart
        </li>
        <li id="LI2">
            <input type="button" class="deleteItem" id="2"> 2nd Item in your cart
        </li>
        <li id="LI3">
            <input type="button" class="deleteItem" id="3"> 3rd Item in your cart
        </li>
    </ul>

Then create the deleteScript.php page to handle the database interaction based on the deleteID you just posted to it.

Sign up to request clarification or add additional context in comments.

10 Comments

I am not sure what you mean by "deleteID="+$(this), what does this do? Shouldn't it be id="+$(this),?
Also I don't get what this does. $("#LI"+$(this)).remove();
@Chris The '$("#LI"+$(this)).remove();' remove the item to delete from the list(It removes the <li> with the id LI?). The 'var dataString = "deleteID="+$(this);' and the ajax function must be inside the click handler($(".deleteItem").click(function());). The example have some errors but it's a start.
@Chris For the firs comment, it can be 'deleteId' or 'id', it depends on how you named the variable in your delete function.
$(this) represents the itme that was clicked.
|
0

Yes, use AJAX to fire a query to delete the item and once your method returns, update and clear the item from your basket.

Comments

0

Create a file which deletes items (e.g. delete-item.php?id=X&user_id=X,) and then reload their list of items. It should be as easy as that.

1 Comment

Thanks, this ended up being a the way I went for other people that see this with the same question.
0

What you're looking for here is an AJAX solution that will POST an HTTP request to your server that results in a DELETE operation on your database.

The simplest way to do this is with one of the many AJAX frameworks out there, such as jQuery, Prototype, or Dojo.

It is vital that you do this with an HTTP POST and not a GET (which is what a simple link would do), because GET requests should never have a side-effect to their main function of retrieving data from a server.

You might want to Google the term CRUD (Create, Read, Update, Delete) along with HTTP to get some insight into this. Or just click here to save finger-work

2 Comments

I am using jQuery as stated in the original question. Thanks though.
Duly noted... but others may not be, so I wanted to include a few other options.

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.