Drag and Drop rows using jquery with ajax example – onlinecode

Drag and Drop rows using jquery with ajax example – onlinecode

In this post we will give you information about Drag and Drop rows using jquery with ajax example – onlinecode. Hear we will give you detail about Drag and Drop rows using jquery with ajax example – onlinecodeAnd how to use it also give you demo for it if it is necessary.

In this example, we will explain to you how to drag and drop rows using jquery with ajax example. let’s start how to create drag and drop jquery example using the jquery UI library.

We are using droppable, draggable and ajax in this example. first, we will connect the MySQL and we divided into two part left and right help through bootstrap. here this example in left known as “All List Country” and right known as “Selected Country”.

We have taken the status field in the database. its value is a 0 and 1. The 0 status data are displaying into the left column and 1 status data are displaying into the right column. when left column country row drag and drop in the right column then will call the ajax that time it’s row data status will be updated into the database. Like that it will be with the right column.

So you can see our example source code.

Database

PHP
-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 16, 2019 at 01:11 PM
-- Server version: 10.1.36-MariaDB
-- PHP Version: 7.2.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: 'drag_drop_div'
--

-- --------------------------------------------------------

--
-- Table structure for table 'country'
--

CREATE TABLE 'country' (
  'id' int(255) NOT NULL,
  'country_name' varchar(255) NOT NULL,
  'status' int(1) NOT NULL DEFAULT '0',
  'created' datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table 'country'
--

INSERT INTO 'country' ('id', 'country_name', 'status', 'created') VALUES
(1, 'Afghanistan', 0, '2019-04-16 00:00:00'),
(2, 'Algeria', 1, '2019-04-15 00:00:00'),
(5, 'Austria', 0, '2019-04-15 00:00:00'),
(6, 'Belgium', 1, '2019-04-17 00:00:00'),
(10, 'Canada', 1, '2019-04-16 00:00:00'),
(13, 'Africa', 1, '2019-04-16 00:00:00'),
(14, 'China', 1, '2019-04-15 00:00:00'),
(17, 'Germany', 1, '2019-04-15 00:00:00'),
(18, 'India', 1, '2019-04-16 00:00:00'),
(21, 'Japan', 1, '2019-04-15 00:00:00'),
(22, 'Mexico', 1, '2019-04-15 00:00:00'),
(25, 'Nepal', 0, '2019-04-09 00:00:00'),
(28, 'New Zealand', 0, '2019-04-15 00:00:00'),
(30, 'Pakistan', 0, '2019-04-15 00:00:00'),
(31, 'Netherlands', 0, '2019-04-15 00:00:00');

--
-- Indexes for dumped tables
--

--
-- Indexes for table 'country'
--
ALTER TABLE 'country'
  ADD PRIMARY KEY ('id');

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table 'country'
--
ALTER TABLE 'country'
  MODIFY 'id' int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=33;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

index.php

PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "drag_drop_div";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Drag and Drop rows using jquery ajax example - onlinecode</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="#">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
   </script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0https://onlinecode.org/js/bootstrap.min.js"> 
  </script>
  <style>
      .product_list-row{
          display:flex;
          margin:30px 0 0 0;
          position:relative;
      }
      .product_list-column{
          flex-grow:1;
          width:45%;
          border: 1px solid #ebeef0;
          background:#fff;
          border-radius:4px;
          margin:0 50px 0 0;
      }

      .leftcolumn.product_list-column ul{
          height: 524px;
          overflow-y:scroll;
      }
      .rightcolumn.product_list-column ul{
          height: 524px;
          overflow-y:scroll;
      }

      .product_list-column:last-child{
          margin-right:0px;
      }
      .connected-sortable{
          margin:0;
          list-style: none;
          width:100%;
          padding:0 10px;
      }
      .connected-sortable li{
          border-bottom: 1px solid #ebeef0;
          position:relative;
          padding: 12px;
          -webkit-transition: transform .25s ease-in-out;
          -moz-transition: transform .25s ease-in-out;
          -o-transition: transform .25s ease-in-out;
          transition: transform .25s ease-in-out;

          -webkit-transition: box-shadow .25s ease-in-out;
          -moz-transition: box-shadow .25s ease-in-out;
          -o-transition: box-shadow .25s ease-in-out;
          transition: box-shadow .25s ease-in-out;
      }
      .connected-sortable li img{
          display:inline-block;
          border: 1px solid #97a3b0;
          vertical-align:middle;
      }
      .connected-sortable li strong{
          display:inline-block;
          font-size:14px;
          padding:0 10px;
          vertical-align:middle;
      }
      .connected-sortable li a.removeItem{
          display:inline-block;
          cursor:pointer;
          border: solid 2px #97a3b0;
          border-radius: 100%;
          width: 24px;
          height: 24px;
          text-align: left;
          ine-height: 20px;
          font-size: 11px;
          font-weight: 800;
          color: #97a3b0 !important;
          padding: 0 0 0 6px;
          position: absolute;
          right: 0;
          top: 50%;
          margin-top: -12px;
      }
      .product_list-column h3{
          font-size:16px;
          text-align: center;
          color:#000;
          font-weight:600;
          padding:15px 20px;
          border-bottom:solid 1px #ebeef0;
      }
      li.draggable-item {
          width: inherit;
          border: 1px solid #fff;
          padding: 15px 20px;
          background-color: #f5f5f5;
          -webkit-transition: transform .25s ease-in-out;
          -moz-transition: transform .25s ease-in-out;
          -o-transition: transform .25s ease-in-out;
          transition: transform .25s ease-in-out;
      }
      /* styles during drag */
      li.draggable-item.ui-sortable-helper {
          background-color: #e5e5e5;
          -webkit-box-shadow: 0 0 8px rgba(53,41,41, .8);
          -moz-box-shadow: 0 0 8px rgba(53,41,41, .8);
          box-shadow: 0 0 8px rgba(53,41,41, .8);
          transform: scale(1.015);
          z-index: 100;
      }
      li.draggable-item.ui-sortable-placeholder {
          background-color: #ddd;
          -moz-box-shadow:    inset 0 0 10px #000000;
          -webkit-box-shadow: inset 0 0 10px #000000;
          box-shadow:         inset 0 0 10px #000000;
      }
  </style>
</head>
<body>

<div >
	<div >
		<div >
		  <h3>All List Country</h3>
		  <ul >
		  <?php
			$sql="SELECT * FROM country where status=0 ORDER by id DESC ";
			$result = mysqli_query($conn, $sql);
			while($row = mysqli_fetch_assoc($result)) {
			?>
			<li  data-itemid="<?php echo $row['id'] ?>"><?php echo $row['country_name'] ?></li> 
		  <?php } ?>
		  </ul>
		</div>
		<div >
            <h3>Selected Country</h3>
			<ul >
				<?php
					$sql="SELECT * FROM country where status=1 ORDER by id DESC ";
					$result = mysqli_query($conn, $sql);
					$rowcount=mysqli_num_rows($result);
					if(!empty($rowcount))
					{
						while($row = mysqli_fetch_assoc($result)) {						
						?>
							<li  data-itemid="<?php echo $row['id']; ?>">
								<?php echo $row['country_name']; ?>
							</li>
						<?php 
						}
					}else{
					?>
						<li >No Selected country Found</li>
					<?php 
					}
					?>
			</ul>
		</div>
	</div>	
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
<script>
$(init);

function init() {
	$( ".droppable-area1, .droppable-area2" ).sortable({
	  connectWith: ".connected-sortable",
	  stack: '.connected-sortable ul'
	}).disableSelection();
}
</script>
<script>
$(document).ready(function(){
   // localStorage.setItem('session_type', '');

    $( ".rightcolumn" ).droppable({
      drop: function( event, ui ) {

          var itemid = ui.draggable.attr('data-itemid');

          $.ajax({
             method: "POST",
             url: "update_item_status.php",
             data:{'itemid': itemid},
          }).done(function( data ) {
			 location.reload();

           });
         }
      });

    $( ".rightcolumn li.no" ).draggable({
        appendTo: "body",
        helper: "clone",
        connectToSortable: ''
    }).droppable({accept: ""});


      $( ".leftcolumn" ).droppable({
          drop: function( event, ui ) {
              var itemid = ui.draggable.attr('data-itemid')
              $.ajax({
                  method: "POST",
                  url: "update_item_status.php",
                  data:{'itemid': itemid},
              }).done(function( data ) {
                  location.reload();
              });
          }
      });
});  
</script>
</body>
</html>

update_item_status.php

PHP
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "drag_drop_div";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

if(!empty($_REQUEST['itemid']))
{
    $sql="SELECT * FROM country where id='".$_REQUEST['itemid']."' ORDER by id DESC ";
    $result = mysqli_query($conn, $sql);
    $rowcount=mysqli_num_rows($result);
    $data = mysqli_fetch_array($result);

    if(!empty($data))
    {
        if($data['status']==0)
        {

        $status=1;
        }
        else{
            $status=0;
        }
        $sql="UPDATE country SET status='".$status."' WHERE id='".$_REQUEST['itemid']."'";
        mysqli_query($conn, $sql);
    }

}
?>

If you like this article then You can see, comment and share this article demo

Show Demo

Please follow and like us:

Hope this code and post will helped you for implement Drag and Drop rows using jquery with ajax example – onlinecode. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

8  +  1  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US