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
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.
10 Comments
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
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