首頁  >  文章  >  後端開發  >  關於PHP+jQuery-ui拖曳浮動層排序並儲存到資料庫實例

關於PHP+jQuery-ui拖曳浮動層排序並儲存到資料庫實例

藏色散人
藏色散人轉載
2020-03-16 13:59:433170瀏覽

PHP jQuery-ui實作的拖曳浮動層排序佈局並將拖曳後的浮動層位置排序結果儲存到資料庫實例。

關於PHP+jQuery-ui拖曳浮動層排序並儲存到資料庫實例

首先引入jQuery庫和jquery-ui.min.js,接著放置一個拖曳時的載入圖片,和從資料庫讀取出來的多個模組拖曳層.modules,及#orderlist用於記錄模組的排序值 

<script type="text/javascript" src="jquery.js"></script>  
<script type=&#39;text/javascript&#39; src=&#39;js/jquery-ui.min.js&#39;></script>
<div id="loader"></div>  
<div id="module_list">  
<input type="hidden" id="orderlist" value="<?php echo $sort; ?>" />  
   <!--?php 
                for ($i = 0; $i < $len; $i++) { 
                    ?-->  
   <div class="modules" title="<?php echo $sort_arr[$i]; ?>">  
    <h3 class="m_title">Module: 
     <!--?php echo $sort_arr[$i]; ?--></h3>  
    <p> 
     <!--?php echo $sort_arr[$i]; ?--></p>  
   </div>  
   <!--?php } ?-->  
   <div class="cl"></div>  
</div>

頁面js:

$(function() { 
    $(".m_title").bind(&#39;mouseover&#39;, 
    function() { 
        $(this).css("cursor", "move") 
    }); 
 
    var $show = $("#loader"); //进度条 
    var $orderlist = $("#orderlist"); 
    var $list = $("#module_list"); 
 
    $list.sortable({ 
        opacity: 0.6, 
        revert: true, 
        cursor: &#39;move&#39;, 
        handle: &#39;.m_title&#39;, 
        update: function() { 
            var new_order = []; 
            $list.children(".modules").each(function() { 
                new_order.push(this.title); 
            }); 
            var newid = new_order.join(&#39;,&#39;); 
            var oldid = $orderlist.val(); 
            $.ajax({ 
                type: "post", 
                url: "update.php", 
                data: { 
                    id: newid, 
                    order: oldid 
                }, 
                //id:新的排列对应的ID,order:原排列顺序 
                beforeSend: function() { 
                    $show.html("<img  src=&#39;images/load.gif&#39; / alt="關於PHP+jQuery-ui拖曳浮動層排序並儲存到資料庫實例" > 正在更新"); 
                }, 
                success: function(msg) { 
                    $show.html(""); 
                } 
            }); 
        } 
    }); 
});

拖曳後儲存到資料庫,ajax.php中的程式碼:

$order = $_POST[&#39;order&#39;]; 
$itemid = trim($_POST[&#39;id&#39;]); 
if (!empty($itemid)) { 
    if ($order != $itemid) { 
        $query = mysql_query("update sortlist set sort=&#39;$itemid&#39; where id=1"); 
        if ($query) { 
            echo $itemid; 
        } else { 
            echo "none"; 
        } 
    } 
}

相關推薦:

PHP影片教學:https://www.php.cn/course/list/29/type/2.html

#

以上是關於PHP+jQuery-ui拖曳浮動層排序並儲存到資料庫實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:cnblogs.com。如有侵權,請聯絡admin@php.cn刪除