PHP - Submit Post When Drag Using jQuery

In this tutorial we will create a Submit Post When Drag Using jQuery. Ajax is a client-side script that communicates to a server/database without the need for a page refresh. It is mostly use by a well known websites like facebook. AJAX is a new technique for creating better, faster, and more interactive web applications. 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_drag, 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_drag");
  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 you 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 - Submit Post When Drag Using jQuery</h3>
  14.                 <hr style="border-top:1px dotted #ccc;"/>
  15.                
  16.                 <div class="col-md-6">
  17.                         <label draggable="true" style="font-size:18px;" class="animal" id="animal1">Elephant</label>
  18.                         <br />
  19.                         <label draggable="true" style="font-size:18px;" class="animal" id="animal2">Zebra</label>
  20.                         <br />
  21.                         <label draggable="true" style="font-size:18px;" class="animal" id="animal3">Lion</label>
  22.                         <br />
  23.                         <label draggable="true" style="font-size:18px;" class="animal" id="animal4">Kangaroo</label>
  24.                         <br />
  25.                         <label draggable="true" style="font-size:18px;" class="animal" id="animal5">Monkey</label>
  26.                 </div>
  27.                 <div class="col-md-6">
  28.                         <div id="drop-zone">
  29.                                 <img src="image/cage.png" height="200"/>
  30.                         </div>
  31.                 </div>
  32.         </div>
  33. <script src="js/jquery-3.2.1.min.js"></script> 
  34. <script src="js/script.js"></script>
  35. </body>
  36. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data to the database server using ajax request. To do that just copy and write this block of codes inside the text editor, then save it as save.php.
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if($_POST['name']){
  5.                 $name = $_POST['name'];
  6.                 mysqli_query($conn, "INSERT INTO `animal` VALUES('', '$name')") or die(mysqli_error());
  7.                 echo "Successfully save data!";
  8.         }
  9. ?>

Creating Main Function

This code contains the main function of the application. This code will store the data after dragging the object in the drop zone. To do that just copy and write this block of codes inside the text editor, then save it inside the js directory as script.js.
  1. $(document).ready(function(){
  2.         $("#drop-zone").on('dragenter', function (e){
  3.                 e.preventDefault();
  4.                 $(this).css('background', '#ccc');
  5.         });
  6.        
  7.         $("#drop-zone").on('dragleave', function (e){
  8.                 e.preventDefault();
  9.                 $(this).css('background', '#fff');
  10.         });
  11.  
  12.         $("#drop-zone").on('dragover', function (e){
  13.                 e.preventDefault();
  14.         });
  15.        
  16.         $(".animal").on('dragstart', function (e){
  17.                 e.originalEvent.dataTransfer.setData("data", e.target.id);
  18.         });
  19.  
  20.         $("#drop-zone").on('drop', function (e){
  21.                 $(this).css('background', '#fff');
  22.                 e.preventDefault();
  23.                 var data = e.originalEvent.dataTransfer.getData("data");
  24.                 var animal = $("#"+data).html();
  25.                
  26.                 $.post( "save.php", { name: animal}, function(data){
  27.                         alert(data)
  28.                 });
  29.         });
  30. });
There you have it we successfully created Submit Post When Drag Using jQuery. 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