PHP - Save Image Data By Dragging Using Ajax

Submitted by: 
Language: 
Visitors have accessed this post 210 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  9. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  10. </a> </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">PHP - Save Image Data By Dragging Using Ajax</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16.  
  17. <div class="col-md-6">
  18. <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;"/>
  19. <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;"/>
  20. <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;"/>
  21. <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;"/>
  22. </div>
  23. <div class="col-md-6">
  24. <div id="drop-zone" style="border:5px dotted #ccc; height:200px; padding-top:50px; padding-left:50px;">
  25. <h2>DROP HERE</h2>
  26. </div>
  27. </div>
  28. <br style="clear:both;"/>
  29. <table class="table table-bordered">
  30. <thead>
  31. <tr>
  32. <th>File Name</th>
  33. <th>File Type</th>
  34. <th>File Path</th>
  35. </tr>
  36. </thead>
  37. <tbody id="result"></tbody>
  38. </table>
  39. </div>
  40. <script src="js/jquery-3.2.1.min.js"></script>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.