0

需要一种让元素显示在 div 中的方法,但是当我使用 ajax 将数据发送到 list.php.it 时它不能工作吗?

PHP:

<?php
mysql_connect('localhost','user','password');
mysql_select_db('fruit');
$days = 3;
for($i=1;$i<=$days;$i++)
{
?>



<ul id="sortable">
    <?php
    $sql = "select * from menu where columnNo = '$i' order by orderNo ASC";
    $result= mysql_query($sql);
    //$row=mysql_fetch_assoc($result);
    //print_r($row);
    while($row=mysql_fetch_assoc($result))
    {
        echo '<li id="list_' . $row['id'] . '">' . $row['title'] . "</li>\n";//
    }

    ?>
    </ul>

<?php
}
?>

jQuery:

$(function(){
     $("ul").sortable({
        connectWith:"ul",
        update:function()
        {           
            serial = $("ul").sortable("serialize");
            $.ajax({
                data: serial,                
                url:"list.php",
                type:"post",
                error:function(){
                    alert("Error!");
                },
                success:function(data){
                    $("#serverResponse").html(data);
                }
            });
            //alert(serial);
        }
     });

    $("#sortable").disableSelection();


});

列表.php

<?php

mysql_connect('localhost','root','xingxing');
mysql_select_db('fruit');

$list = $_POST['id'];

for($i=0;$i<count($menu);$i++)
{


    $sql =  "update menu set orderNo= '$i' where id = '$menu[$i]'";
    mysql_query($sql);
}

?>

但是,list.php 不起作用。我不知道为什么。有人可以帮助我吗?谢谢!

4

1 回答 1

1

不确定这是否会有所帮助,但您尝试更新每个重新排序事件(这真的有必要吗?)。

在更新后的几秒钟空闲时间后使用 javascript 超时更新可能是明智的。我个人使用这种方法,它使事情运行得更顺畅。

在您的 jQuery .sortable() 设置中添加以下内容:

start: function(event, ui){
    clearTimeout(allow_update);
},
update: function(event, ui){
    allow_update = window.setTimeout(UpdateOrdering, 3000);
}

然后添加var allow_update = null; 某处和一个UpdateOrdering函数,该函数在超时到期(在我的情况下为 3000 毫秒)时调用,其中包含您的更新功能。

希望这可以帮助 :)

于 2009-09-15T12:50:36.050 回答