简体   繁体   中英

The ajax data is not sending on update jQuery sortable

There is this piece of javascript/jQuery code that uses the sortable feature of jQuery. I use it to sort an unknown amount of divs, although currently I'm trying to send the data to the database with ajax:

Javascript with jQuery.sortable & ajax

var lst, pre;

$(".sortlist").sortable({
    start:function(event, ui){
        pre = ui.item.index();
    },
    axis: 'y',
    handle: '.handle',
    update: function (event, ui) {
        var data = $("#list1").sortable("toArray");

        console.log($("#list1").serializeArray = data);

        $.ajax({
            data: data,
            type: 'POST',
            url: 'backend/components/reorder-cards.php'
        });
    },
    stop: function(event, ui) {
        lst = $(this).attr('id');
        post = ui.item.index();
        other = (lst == 'list1') ? 'list2' : 'list1';
        //Use insertBefore if moving UP, or insertAfter if moving DOWN
        if (post > pre) {
            $('#'+other+ ' div:eq(' +pre+ ')').insertAfter('#'+other+ ' div:eq(' +post+ ')');
        } else {
            $('#'+other+ ' div:eq(' +pre+ ')').insertBefore('#'+other+ ' div:eq(' +post+ ')');
        }
    }
}).disableSelection();

Every update it should use the code to do an INSERT in the php file called reorder-cards.php:

include('../../config/connect.php');
$data = $_POST['include('../../config/connect.php');
$data = $_POST['data'];

$insertdata = $conn->prepare("INSERT INTO pageOrder (order) VALUES (:order)");
$insertdata->bindParam(':order', $data, PDO::PARAM_STR);
$insertdata->execute();'];

$insertdata = $conn->prepare("INSERT INTO pageOrder (order) VALUES (:order)");
$insertdata->bindParam(':order', $data, PDO::PARAM_STR);
$insertdata->execute();

However it's not sending anything to the database, I've never gotten an insert via ajax to work. Can somebody tell me what I'm doing wrong?

使用DevTools(例如在Chrome中)检查您是否发送了正确的Ajax请求,因此更容易查找问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM