Today, We want to share with you PHP Dynamically Add Remove Images Upload using JQuery Ajax.In this post we will show you preview and remove image before upload jquery, hear for multiple image upload with preview and delete jquery php we will give you demo and example for implement.In this post, we will learn about jquery multiple image upload with preview and delete demo with an example.
PHP Dynamically Add Remove Images Upload using JQuery Ajax
There are the Following The simple About PHP Dynamically Add Remove Images Upload using JQuery Ajax Full Information With Example and source code.
As I will cover this Post with live Working example to develop delete image from folder using jquery, so the ajax delete image php for this example is following below.
Add Remove image upload with preview Dynamically using jQuery
multiple image upload with preview and delete option using jquery. With the use of jQuery AJAX, We can step by step upload server side using PHP the selected All the file to your server and display all the files preview of it. If We can like to dynamic add a remove(delete) button to delete the file on servser side without any page load then We again required to use jquery AJAX request call to delete or remove(unlink) the file from the server.
Step 1: HTML Interface
index.html
<!doctype html> <html> <head> <title>jquery multiple image upload with preview and delete demo</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <style type='text/css'> .blackcard{ margin: 10px; } .preview{ width: 100px; float: left; margin-right: 5px; border: 2px solid blue; border-radius: 5px; padding: 6px; } /* Remove or Delete Images */ .preview span{ border: 2px solid green; display: inline-block; width: 98%; text-align: center; color: green; } .preview span:hover{ cursor: pointer; } </style> </head> <body > <h2>how to insert image in database using ajax in php</h2> <form method="post" action="" enctype="multipart/form-data" id="myform"> <input type="file" id="file" name="file" /> <input type="button" class="button" value="Upload" id="fileimg_uploads"> </form> <!-- Image element blackcard --> <div class="blackcard"></div> <a href="https://www.pakainfo.com/" target="_blank" alt="pakainfo" title="pakainfo">Free Download Example - Pakainfo.com</a> <!-- Script --> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js' type='text/javascript'></script> <script type='text/javascript'> $(document).ready(function(){ // Upload $("#fileimg_uploads").click(function(){ var fimgdata = new FormData(); var files = $('#file')[0].files[0]; fimgdata.append('file',files); fimgdata.append('storedata',1); //jQuery AJAX request (post Methods) $.ajax({ url: 'addremove.php', type: 'post', data: fimgdata, contentType: false, processData: false, success: function(response){ if(response != 0){ var total = $('.blackcard .preview').length; total = Number(total) + 1; // Show image preview with Delete button $('.blackcard').append("<div class='preview' id='preview_"+total+"' ><img src='"+response+"' width='100' height='100'><span class='delete' id='delete_"+total+"'>Delete</span></div>"); }else{ alert('file not uploaded'); } } }); }); // Remove blackcard file $('.blackcard').on('click','.preview .delete',function(){ var id = this.id; var split_id = id.split('_'); var num = split_id[1]; // Get image source var imgElement_src = $( '#preview_'+num+' img' ).attr("src"); // AJAX request console.log(imgElement_src); $.ajax({ url: 'addremove.php', type: 'post', data: {path: imgElement_src,storedata: 2}, success: function(response){ // Remove <div > if(response == 1){ $('#preview_'+num).remove(); } } }); }); }); </script> </body> </html>
Setp 2 : Upload FILE On Server Side
addremove.php
<?php $get_files_data = $_POST['storedata']; // Upload file if($get_files_data == 1){ $filename = $_FILES['file']['name']; /* destination_path */ $destination_path = "upload/".$filename; $isUploded = 1; $flImgTypeCheck = pathinfo($destination_path,PATHINFO_EXTENSION); // Check FILE or image format if($flImgTypeCheck != "jpg" && $flImgTypeCheck != "png" && $flImgTypeCheck != "jpeg" && $flImgTypeCheck != "gif" ) { $isUploded = 0; } if($isUploded == 0){ echo 0; }else{ /* Upload with Move files or Images */ if(move_uploaded_file($_FILES['file']['tmp_name'],$destination_path)){ echo $destination_path; }else{ echo 0; } } exit; } // Remove or unlink file if($get_files_data == 2){ $get_path = $_POST['path']; $res_data = 0; // Check file exist or not if( file_exists($get_path) ){ // Remove file unlink($get_path); // Set res_data $res_data = 1; }else{ // Set res_data $res_data = 0; } // Return res_data echo $res_data; exit; }
Angular 6 CRUD Operations Application Tutorials
Read :
Summary
You can also read about AngularJS, ASP.NET, VueJs, PHP.
I hope you get an idea about PHP Dynamically Add Remove Images Upload using JQuery Ajax.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.