Drag and Drop rows using jquery with ajax example
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | -- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | <?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 - XpertPhp</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css"> <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.0/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 class="container"> <div class="row product_list-row"> <div class="col-lg-6 leftcolumn product_list-column ui-droppable"> <h3>All List Country</h3> <ul class="connected-sortable droppable-area1 ui-sortable"> <?php $sql="SELECT * FROM country where status=0 ORDER by id DESC "; $result = mysqli_query($conn, $sql); while($row = mysqli_fetch_assoc($result)) { ?> <li class="draggable-item" data-itemid="<?php echo $row['id'] ?>"><?php echo $row['country_name'] ?></li> <?php } ?> </ul> </div> <div class="col-lg-6 rightcolumn product_list-column ui-droppable"> <h3>Selected Country</h3> <ul class="connected-sortable droppable-area2 ui-sortable"> <?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 class="draggable-item" data-itemid="<?php echo $row['id']; ?>"> <?php echo $row['country_name']; ?> </li> <?php } }else{ ?> <li class="draggable-item no">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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?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