简体   繁体   中英

An AJAX/MySQL/PHP question

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.

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.

是的,使用AJAX触发查询以删除该项目,一旦方法返回,请更新并清除购物篮中的该项目。

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM