Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos

Home » Php » Drag and Drop Reorder Items with jQuery, PHP & MySQL

Drag and Drop Reorder Items with jQuery, PHP & MySQL

Drag and Drop Reorder Items with jQuery, PHP & MySQL

Posted on March 21, 2022November 20, 2022 By XpertPhp

In this tutorial, we will explain to you how to create drag and drop reorder items with jQuery, PHP & MySQL.
If you want to drag and drop reorder elements, you should use the jquery UI library because you can drag and drop elements(jquery UI drag and drop list) easily with the help of jquery UI.

Create MySQL Database Table

In this step, We will create the MySQL table for reordering items. so you can see the below example.

1
2
3
4
5
6
7
8
9
CREATE TABLE `items` (
  `id` int(11) NOT NULL,
  `item_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `priority` int(5) NOT NULL DEFAULT '0',
  `status` enum('1','0') COLLATE utf8_unicode_ci NOT NULL DEFAULT '1',
  `created` datetime NOT NULL,
  `modified` datetime NOT NULL,
   PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Connect to Database

Create a new connect.php file. The code below is to provide how to connect with the database.
The following code is used to connect MySQL from PHP. It requires hostname, database username, database password, and database name.

1
2
3
4
5
6
7
8
9
10
<?php
 
$hostname="localhost";
$username="root";
$password="test";
$database="register";
 
$conn = mysqli_connect($hostname,$username,$password,$database);
?>

Include jQuery and jQuery UI Library

1
2
3
<link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>

Display Items from the database

1
2
3
4
5
6
7
8
9
10
11
<ul id="sortable">
<?php
include("connect.php");
$sql_query = "SELECT id, item_name FROM items ORDER BY priority";
$result = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));
while( $row = mysqli_fetch_assoc($result))
{ ?>
<li class="ui-state-default" id="<?php echo $row["id"]; ?>"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span><?php echo $row["item_name"]; ?></li>
  <?php
} ?>
</ul>

Implement Drag and Drop with jQuery UI

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
  <script>
$(function() {
$("#sortable").sortable({
update: function( event, ui ) {
updateOrder();
}
});  
});
  
function updateOrder() {
var item_order = new Array();
$('#sortable li').each(function() {
item_order.push($(this).attr("id"));
});
var order_string = 'order='+item_order;
$.ajax({
type: "POST",
url: "update_order.php",
data: order_string,
cache: false,
success: function(data){
}
});
}
  </script>
See also  How to Limit the Number of Characters per line in textarea

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
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Drag and Drop Reorder Items with jQuery, PHP & MySQL - XpertPhp</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
  <style>
  #sortable { list-style-type: none; margin: 0 auto; padding: 0; width: 60%; }
  #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
  #sortable li span { position: absolute; margin-left: -1.3em; }
  </style>
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
  <script>
$(function() {
$("#sortable").sortable({
update: function( event, ui ) {
updateOrder();
}
});  
});
  
function updateOrder() {
var item_order = new Array();
$('#sortable li').each(function() {
item_order.push($(this).attr("id"));
});
var order_string = 'order='+item_order;
$.ajax({
type: "POST",
url: "update_order.php",
data: order_string,
cache: false,
success: function(data){
}
});
}
  </script>
</head>
<body>
<ul id="sortable">
<?php
include("connect.php");
$sql_query = "SELECT id, item_name FROM items ORDER BY priority";
$result = mysqli_query($conn, $sql_query) or die("database error:". mysqli_error($conn));
while( $row = mysqli_fetch_assoc($result))
{ ?>
<li class="ui-state-default" id="<?php echo $row["id"]; ?>"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span><?php echo $row["item_name"]; ?></li>
  <?php
} ?>
</ul>
</body>
</html>

Update Order into MySQL Database Table

1
2
3
4
5
6
7
8
9
10
<?php
if(isset($_POST["order"])) {
include("connect.php");
$order  = explode(",",$_POST["order"]);
for($i=0; $i < count($order);$i++) {
$sql = "UPDATE items SET priority='" . $i . "' WHERE id=". $order[$i];
mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
}
}
?>

Ajax, Jquery, Php

Post navigation

Previous Post: Laravel 9 Toastr Notifications Example Tutorial
Next Post: how to get current date and time in php

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com