通过ajax&发送数据到PHP脚本JQuery的

时间:2012-02-04 16:24:35

标签: php jquery ajax post

作为JQuery的新手,并试图了解有关发布数据的不同方式的所有帖子,我仍然不知道为什么我的功能不起作用。任何帮助都会受到欢迎,因为我花了几天时间试图让它发挥作用。

我有一个可排序的ul列表:

            <ul class="gallery" id ="gallery">
                <li id="item_59"><a href="/images/pages/apartamentos/155/image115_7.jpg" title=""><img src="/images/pages/apartamentos/155/image115_7.jpg"  alt="" /></a></li>
                <li id="item_61"><a href="/images/pages/apartamentos/155/image115_6.jpg" title=""><img src="/images/pages/apartamentos/155/image115_6.jpg"  alt="" /></a></li>
                <li id="item_62"><a href="/images/pages/apartamentos/155/image115_3.jpg" title=""><img src="/images/pages/apartamentos/155/image115_3.jpg"  alt="" /></a></li>
                <li id="item_63"><a href="/images/pages/apartamentos/155/image115_5.jpg" title=""><img src="/images/pages/apartamentos/155/image115_5.jpg"  alt="" /></a></li>
                <li id="item_64"><a href="/images/pages/apartamentos/155/image115_2.jpg" title=""><img src="/images/pages/apartamentos/155/image115_2.jpg"  alt="" /></a></li>
                <li id="item_65"><a href="/images/pages/apartamentos/155/image115_1.jpg" title=""><img src="/images/pages/apartamentos/155/image115_1.jpg"  alt="" /></a></li>
                <li id="item_66"><a href="/images/pages/apartamentos/155/image115_4.jpg" title=""><img src="/images/pages/apartamentos/155/image115_4.jpg"  alt="" /></a></li>
            </ul>

然后在同一个文档中我有这个Jquery:

   <script type="text/javascript">

$(document).ready(function() {


        $(function(){
        $("#gallery").sortable({stop:function(event, ui) {

        $.ajax({
        type: "GET",
        url: "image_order.php",
        data: $("#gallery").sortable("serialize"),

                    success: function(data) {
                        if (data) {
                            $('#msg').html('Success');
                        } 
                    },
                    error: function () {
                        $('#msg').html('Failed');
                    }

                })
                }
            });

    }
}


</script>

然后我有一个image_order.php:

 foreach ($_GET['item'] as $position => $item) {
$query_edit = "UPDATE `image_order` SET `position` = $position WHERE `id` = $item";
mysql_query($query_edit, $ith) or die(mysql_error());
$sql[] = "UPDATE `image_order` SET `position` = $position WHERE `id` = $item";
}

echo json_encode($sql);

当你手工进入时 image_order.php项[] = 1&安培;?项目[] = 2及项[] = 3及项[] = 4 返回:

["UPDATE `image_order` SET `position` = 0 WHERE `id` = 1","UPDATE `image_order` SET `position` = 1 WHERE `id` = 2","UPDATE `image_order` SET `position` = 2 WHERE `id` = 3","UPDATE `image_order` SET `position` = 3 WHERE `id` = 4"] 

我有一个类似的json函数可以在这个页面上运行,我看到在控制台中调用的是url而不是这个。

我也试过$(“ul.gallery”)。sortable({stop:function(event,ui){...但是这也不起作用。

帮助会很棒......请...

1 个答案:

答案 0 :(得分:0)

我必须绑定sortstop事件(RTM):

http://jqueryui.com/demos/sortable/#event-stop

  $( "ul.gallery" ).bind( "sortstop", function(event, ui) {
        $.ajax({
        type: "GET",
        url: "image_order.php",
        data: $("ul.gallery").sortable("serialize"),

                    success: function(data) {
                        if (data) {
                            $('#msg').html('Success');
                        } 
                    },
                    error: function () {
                        $('#msg').html('Failed');
                    }

                })

            });