PHP - Save Image Data By Dragging Using Ajax

In this tutorial we will create a Save Image Data By Dragging using Ajax. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. Using Ajax, data could then be passed between the browser and the server, using the XMLHttpRequest API, without having to reload the web page. So Let's do the coding...

Getting started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And this is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_image, after that click Import then locate the database file inside the folder of the application then click ok. tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.
  1. <?php
  2. $conn = mysqli_connect("localhost", "root", "", "db_image");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  4. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  5. <body>
  6. <nav class="navbar navbar-default">
  7. <div class="container-fluid">
  8. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  9. </div>
  10. </nav>
  11. <div class="col-md-3"></div>
  12. <div class="col-md-6 well">
  13. <h3 class="text-primary">PHP - Save Image Data By Dragging Using Ajax</h3>
  14. <hr style="border-top:1px dotted #ccc;"/>
  15.  
  16. <div class="col-md-6">
  17. <img src="images/image1.jpeg" draggable="true" class="images" id="image1" name="image1.jpeg" height="100" width="120" style="border:1px SOLID #000; margin:10px;"/>
  18. <img src="images/image2.jpeg" draggable="true" class="images" id="image2" name="image2.jpeg" height="100" width="120" style="border:1px SOLID #000; margin:10px;"/>
  19. <img src="images/image3.jpg" draggable="true" class="images" id="image3" name="image3.jpg" height="100" width="120" style="border:1px SOLID #000; margin:10px;"/>
  20. <img src="images/image4.jpg" draggable="true" class="images" id="image4" name="image4.jpg" height="100" width="120" style="border:1px SOLID #000; margin:10px;"/>
  21. </div>
  22. <div class="col-md-6">
  23. <div id="drop-zone" style="border:5px dotted #ccc; height:200px; padding-top:50px; padding-left:50px;">
  24. <h2>DROP HERE</h2>
  25. </div>
  26. </div>
  27. <br style="clear:both;"/>
  28. <table class="table table-bordered">
  29. <thead>
  30. <tr>
  31. <th>File Name</th>
  32. <th>File Type</th>
  33. <th>File Path</th>
  34. </tr>
  35. </thead>
  36. <tbody id="result"></tbody>
  37. </table>
  38. </div>
  39. <script src="js/jquery-3.2.1.min.js"></script>
  40. <script src="js/script.js"></script>
  41. </body>
  42. </html>

Creating PHP Query

This code contains the php query of the application. This code will store the image data to the database server, and also can retrieve it then display in the table. To do that just copy and write this block of codes inside the text editor, then save it as show below. save.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $image_name = $_POST['image_name'];
  5. $image_type = $_POST['image_type'];
  6. $image_path = $_POST['image_path'];
  7.  
  8. mysqli_query($conn, "INSERT INTO `image` VALUES('', '$image_name', '$image_type', '$image_path')") or die(mysqli_error());
  9.  
  10. echo "Successfully saved!";
  11. ?>
data.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. if(ISSET($_POST['res'])){
  5.  
  6. $query = mysqli_query($conn, "SELECT * FROM `image`") or die(mysqli_error());
  7. while($fetch = mysqli_fetch_array($query)){
  8. ?>
  9. <tr>
  10. <td><?php echo $fetch['image_name']?></td>
  11. <td><?php echo $fetch['image_type']?></td>
  12. <td><?php echo $fetch['image_path']?></td>
  13. </tr>
  14. <?php
  15. }
  16. }
  17. ?>

Creating The Script

This is where the main function of the application. This code can drag the image in the drop zone, then will get the image data and send ajax request to the server. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2. getData();
  3.  
  4. $("#drop-zone").on('dragenter', function (e){
  5. e.preventDefault();
  6. $(this).css('background', '#ccc');
  7. });
  8.  
  9. $("#drop-zone").on('dragleave', function (e){
  10. e.preventDefault();
  11. $(this).css('background', '#fff');
  12. });
  13.  
  14. $("#drop-zone").on('dragover', function (e){
  15. e.preventDefault();
  16. });
  17.  
  18. $(".images").on('dragstart', function (e){
  19. e.originalEvent.dataTransfer.setData("data", e.target.id);
  20. });
  21.  
  22. $("#drop-zone").on('drop', function (e){
  23. $(this).css('background', '#fff');
  24. e.preventDefault();
  25. var data = e.originalEvent.dataTransfer.getData("data");
  26. var image_name = $("#"+data).attr("name");
  27. var image = $("#"+data).attr("name");
  28. var splt = image.split(".");
  29. var image_type = splt[1];
  30. var image_path = $("#"+data).attr("src");
  31. $.ajax({
  32. url: 'save.php',
  33. type: 'POST',
  34. data: {image_name: image_name, image_type: image_type, image_path: image_path},
  35. success: function(data){
  36. alert(data);
  37. getData();
  38. }
  39. });
  40. });
  41. });
  42.  
  43. function getData(){
  44. $.ajax({
  45. url: 'data.php',
  46. type: 'POST',
  47. data: {res: 1},
  48. success: function(data){
  49. $('#result').html(data);
  50. }
  51. });
  52. }
There you have it we successfully created Save Image Data By Dragging Using Ajax using Ajax. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Add new comment