I have a profile page containing a series of images. I want to use jQuery to allow the user to delete an image from the server and refresh the page without reloading the entire page. When it succeeds, it will remove the image containing the div from the page. My delete function is PHP; pretty simple:
delete.php
<?php if (isset($_POST['id'])) { if (unlink($_POST['id'])) { echo "success"; } else { echo "failure"; } } ?>
(User authentication already exists to get them on the page that calls delete.php.)
Here is the html of one displayed image - there can be up to five such blocks one after another:
<div class="box"> <img src="uploads/t_10DOT_22C_1111_1300370702_3.jpg" /> <h5><a rel="external" href="uploads/10DOT_22C_1111_1300370702_3.jpg">See full version</a></h5> <a href="#" id="10DOT_22C_1111_1300370702_3.jpg" class="delete" onclick="return ConfirmDelete();" >x</a> <div class="clear"></div> </div>
My jQuery so far looks something like this:
$(document).ready(function() { $('#load').hide(); }); $(function() { $(".delete").click(function() { $('#load').fadeIn(); var commentContainer = $(this).parent(); var id = $(this).attr("id"); var string = 'id='+ id ; $.ajax({ type: "POST", url: "delete.php", data: string, cache: false, success: function(data){ commentContainer.slideUp('slow', function() {$(this).remove();}); $('#load').fadeOut(); } }); return false; }); });
The part I'm connected to is the ajax post. How does part of success really work? What do I need to do in my php file so that ajax knows if success was successful or unsuccessful?
EmmyS
source share