PHP/jQuery:检索新的可排序值


PHP/jQuery: Retrieve new sortable value

我知道有很多关于如何从JQuery可排序函数中检索新值的指南。但这并不是我想要的。需要在php变量中添加新值,以便我可以在屏幕上打印它或在按下按钮时更新我的mysql数据库。我不想在两个单独的文件中工作。因此我使用了post变量

下面是我到目前为止编写的代码(将其保存到php文件中):

<?php
echo '
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>
<style>
  #sortable { list-style-type: none; margin: 0; padding: 0; width: 100%; }
  #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>
  $(function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  });
</script>
</head>
<body>
<ul id="sortable">';
$array = str_split("ABCDEFGHI", 3);
for($i = 0; $i < count($array); $i++) {
  echo '<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' . $array[$i] . '</li>';
}
echo '
</ul><br>
<form action="" method="post">
  <input type="submit" name="submit" value="Show order">
</form>
</body>
</html>';
if(isset($_POST['submit'])) {
  // How to print the new order value?
}
?>

如果您使用隐藏的输入字段作为数组,那么当您提交表单时,您将拥有新排序的值:

为您的<li>:

echo '<li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' . $array[$i] . '<input type="hidden" name="pos[]" value="'.$array[$i].'"></li>';

输出结果:

if(isset($_POST['submit'])) {
    foreach($_POST['pos'] as $value){
        echo $value.'<br>';
    }
    echo '<pre>',print_r($_POST['pos']),'</pre>';
}

li被洗牌时,poskey总是会改变。提交后,它们将按照检索的方式排序(从上到下)。