doujianglin6704 2014-11-20 07:52
浏览 53
已采纳

jQueryUI.Sortable更新,不使用Ajax作为普通表单提交给PHP

I would like to sort a table with data row from MySQL then change the order and submit it.
I used jQueryUI.sortable to make those tr tag (row) draggable.
But when I submitting the form, some of them didn't changed order.

Why? I tried to figure it out, I var_dump the data I submitted and I found a problem: The tr tag (row) I moved from the original order, won't pass to PHP so var_dump will not show the row ID.

To make it easier to understand, I post my code here:

HTML Code

<table>
    <thead>
        <tr>
            <th>Subject</th>
            <th>Content</th>
        </tr>
    </thead>
    <tbody id="sortable">
        <tr>
            <td>
                Hello World
                <input name="displayorder[]" type="hidden" value="1" />
            </td>
            <td>I come from Mars!</td>
        </tr>
        <tr>
            <td>
                Hello Mars
                <input name="displayorder[]" type="hidden" value="2" />
            </td>
            <td>I come from Jupiter!</td>
        </tr>
        <tr>
            <td>
                Hello StackOverflow
                <input name="displayorder[]" type="hidden" value="3" />
            </td>
            <td>I come from North Korea ...</td>
        </tr>
    </tbody>
    <tbody>
        <tr>
            <td colspan="2"><input type="submit" value="Submit!" />
        </tr>
    </tbody>
</table>

I omitted the form content cause it is not important

JavaScript (Sortable Library loaded)

$(document).ready(function() {
    $('#sortable').sortable({
        helper: fixHelper,
        axis: 'y',
        opacity: 0.6,
    }).disableSelection();
});

var fixHelper = function(e, ui) {
    ui.children().each(function() {
        $(this).width($(this).width());
    });
    return ui;
};

PHP

$displayorder = $_POST["displayorder"];
if($displayorder != "") {
    $order = 1;
    foreach($displayorder as $value) {
        mysql_query("UPDATE message SET displayorder=$order WHERE announcementid=$value");
        $order++;
    }
}

I will prefer not using Ajax to do this because I have dozens of similar page to do the same task.
Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongxin991209 2014-11-20 09:16
    关注

    Well I decided to code it every page.
    The code now:

    JavaScript

    $(document).ready(function() {
        $('#sortable').sortable({
            helper: fixHelper,
            axis: 'y',
            opacity: 0.4,
            update: function(event, ui){
                var data = $(this).sortable('serialize');
    
                $.ajax({
                    data: data,
                    type: 'POST',
                    url:  '/update.php?action=displayorder'
                });         
            },
        }).disableSelection();
    });
    
    var fixHelper = function(e, ui) {
        ui.children().each(function() {
            $(this).width($(this).width());
        });
        return ui;
    };
    

    PHP

    foreach($_POST["displayorder"] as $i => $value) {
        mysql_query("UPDATE message SET displayorder=$i WHERE announcementid=$value");
        $i++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛