I have the following code that saves the reordering of a list.
INDEX.PHP
<?php
require_once ("Class.Drag.php");
$auth_task = new Drag();
$list_order = strip_tags($_POST['list_order']);
$res=$auth_task->update($list_order);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title></title>
<link rel="stylesheet" href="css/style.css" />
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui-1.10.4.custom.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>
<div class="container">
<div class="content">
<ul id="sortable">
<?php
echo $auth_task->select();
?>
</ul>
</div><!-- content -->
</div><!-- container -->
</body>
</html>
CLASS.DRAP.PHP
public function update($list_order){
$list = explode(',' , $list_order);
$i = 1 ;
foreach($list as $id) {
try {
$stmt = $this->conn->prepare('UPDATE items SET item_order = :item_order WHERE id = :id');
$stmt->bindParam(':item_order', $i, PDO::PARAM_INT);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
}catch (PDOException $exception){
header("Location: ./error.php?err=Cant-Order");
echo 'Erro: '.$exception->getMessage();
return null;
}
$i++ ;
}
}
SCRIPTDRAG.JS
$(function() {
$('#sortable').sortable({
axis: 'y',
opacity: 0.7,
handle: 'span',
update: function(event, ui) {
var list_sortable = $(this).sortable('toArray').toString();
$.ajax({
url: 'index.php',
type: 'POST',
data: {list_order:list_sortable},
success: function(data) {
success_message('Gravado');
},
error: function(data){
failure_message('Erro');
}
});
}
});
});
Only my posting is empty, as I do not use it
$list_order = strip_tags($_POST['list_order']);
$res=$auth_task->update($list_order);
There is some way to send the $ list_order directly to the class by ajax, without having to do the post in the index.
Notice: Undefined index: list_order , for the empty post.