使用 ajax 将对象数组发送到 php 并提交给 SQL


Send objects array to php with ajax and submit to SQL

我正在尝试创建一个可嵌套的列表,我可以通过拖放对其进行重新排序,并且到目前为止做得很好,非常感谢您;-),但是...

一切都在正常工作,但我希望能够在我放下项目时通过 php 将 tor 顺序保存到数据库中,但不确定该怎么做?

当我放下一个项目时,我得到一个对象数组,如下所示:

[对象、对象、对象

、对象

、对象、

每个对象和项以及一个项 ID:

<li data-item="<?=$row['name']?>" data-item-id="<?=$row['fk_module_id']?>">

如何通过 ajax 将其传递给 php,以及如何在 php 脚本中打开对象?

以下是处理 dragDrop 部分的 jQuery:

$("#modules-active").on('nestable-stop', function(ev)
{
    var serialized = $(this).data('nestable').serialize(),
    str = '';
    console.log( serialized );
    console.log( $(this).data('nestable').list() );
    $.post( "savelist.php", { list: serialized }).done(function( data ) { 
    });
});

回顾一下:如何将其正确传递给 php,以及如何在 php 页面上打开并保存它?

任何帮助都值得赞赏,提前感谢:-)

<li class='data' data-item="<?=$row['name']?>" data-item-id="<?=$row['fk_module_id']?>">

jQuery(document).ready(function($){
    url = " Your php file url/savelist.php";
    var allVals = [];
    $('.btn').click(function(){
        $('.data').each(function() {
            allVals.push($(this).val());
        });
        $.post(url,{ array : allVals }, function(response,status){
            if(status == 'success'){
                //ur stuffs
                return status;
            }
        })
    })
})

它可能会帮助你..