Add Delete Image using jQuery

Add Delete Image using jQuery with PHP script via Ajax

In this tutorial, we are going to create Add, Delete Image using jQuery with PHP script via Ajax. In the previous tutorial, we already created Ajax Select and Upload Multiple Images and File Image Upload With Gallery Using PHP but they don’t have delete option function in the last tutorial. We are going to add an image using PHP file upload script into the target folder or location. If we want to delete, we are going to use also the PHP script to remove the image file from the location or folder. Let’s start coding.

PHP Add Script

This PHP adds file script is to add a certain file image upload to the location or folder. If the image successful to upload, it will display the image to the web browser.
  1. <script type="text/javascript">
  2. $(document).ready(function (e) {
  3.         $("#form_To_Upload").on('submit',(function(e) {
  4.                 e.preventDefault();
  5.                 $.ajax({
  6.                 url: "upload.php",
  7.                         type: "POST",
  8.                         data:  new FormData(this),
  9.                         contentType: false,
  10.             cache: false,
  11.                         processData:false,
  12.                         success: function(data)
  13.                     {
  14.                         $("#target_Container").html(data);
  15.                     },
  16.                         error: function(){}            
  17.            });
  18.         }));   
  19. });
  20. </script>

Delete Statement using jQuery Ajax

It contains Ajax script and deletes statement in PHP file to delete the image displaying in the web browser. Once the image deleted, the container of the image replaced by the text of “No Image”.
  1. <script type="text/javascript">
  2. function deleteImage(path) {
  3.         $.ajax({
  4.                 url: "delete.php",
  5.                 type: "POST",
  6.                 data:  {'path':path},
  7.                 success: function(data){
  8.                         $("#target_Container").html('<div class="no-image">No Image</div>');
  9.                 },
  10.                 error: function(){}            
  11.         });
  12. }
  13. </script>

Result

Result Result Note: You can customize your file upload whatever your desired design by changing the CSS. Hope that this simple yet useful tutorials that I created may help you to your future projects. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment