【问题标题】:JQuery Sortable PHP to DatabaseJQuery 可排序 PHP 到数据库
【发布时间】:2015-02-04 03:53:27
【问题描述】:

所以我有 2 个盒子。在左侧,我有一个从数据库中提取的项目列表,我可以将其拖放到右侧。这很好用。我这辈子都不知道如何让它发布右侧列表的数据,我想我已经尝试了谷歌本周提供的所有示例。

当我执行 print_r($_POST);在这个提交的页面上,我得到了 Array ( ),其中没有任何内容。它似乎没有抓取 ID 并对其进行序列化。

有没有人有这方面的经验看看我遗漏了什么?

  <script>
  $(function() {
    $( "ul.droptrue" ).sortable({
        connectWith: "ul"
    });

    $( "ul.dropfalse" ).sortable({
      connectWith: "ul",
      dropOnEmpty: true
    });

    $( "#sortable1, #sortable2" ).disableSelection();
  });

      $( "#sortable2" ).sortable({
    axis: 'y',
    handle : '.handle', 
    update: function (event, ui) {
        var data = $(this).sortable('serialize');
        console.log(data);

        // POST to server using $.post or $.ajax
        $.ajax({
            data: data,
            type: 'POST',
            url: 'setlists-edit-process.php'
        });
    }
});

  </script>

<ul id="sortable1" class="droptrue">
<?php
$_GET['setlist_id'];
$sql = "SELECT material_id, material_name FROM material WHERE user_id=$session_id";
$result = mysqli_query($conn, $sql);

if (!$result) {
    printf("Error: %s\n", mysqli_error($conn));
    exit();
}

while($row = mysqli_fetch_array($result))
{
echo "<li id=" . $row['material_id'] . ">" . $row['material_id'] . " | " . $row['material_name'] . "</li>";
}

?>
</ul>

<ul id="sortable2" class="dropfalse">

</ul>

【问题讨论】:

  • 你的 console.log(data) 显示什么?
  • setlists-edit.php?setlist_id=8:16 Uncaught SyntaxError: Unexpected token & jquery.flot.js:711 Uncaught Invalid dimensions for plot, width = null, height = null
  • 我还以为是参数字符串?
  • 摆脱了其中一个错误。 console.log 现在显示:function log() { [native code] }
  • Check out serialize 另请注意,&lt;li&gt; 元素的 id 必须包含下划线。

标签: php jquery jquery-ui jquery-ui-sortable


【解决方案1】:

替换这个:

while($row = mysqli_fetch_array($result))
{
echo "<li id=" . $row['material_id'] . ">" . $row['material_id'] . " | " . $row['material_name'] . "</li>";
}

与:

while($row = mysqli_fetch_array($result))
{
echo '<li id="material_' . $row['material_id'] . '">' . $row['material_id'] . " | " . $row['material_name'] . "</li>";
}

Sortable 期望 ID 的格式为 setname_number

此外,您的代码输出格式为id=abc(无引号),而应该是带有引号的id="abc"

【讨论】:

  • 谢谢,它现在显示如下列表项:
  • 1 |将一位女士锯成两半
  • 。控制台仍然显示为:function log() { [native code] }
  • @Topher 你可以尝试用var data = $("#sortable2").sortable('serialize'); var data2 = $("#sortable2").sortable('toArray'); console.log( 'Data Serialize:'); console.log( data ); console.log( 'Data toArray:'); console.log( data2 );替换var data = $(this).sortable('serialize'); console.log( data );
  • 已添加。我现在用它做什么?
  • @Topher 你能把结果贴在这里吗?你还是只看到 log(){ [native code] } 吗?
  • @Topher 哦,天哪!您的浏览器甚至没有安装控制台。您可以尝试在所有情况下将console.log() 替换为alert() 吗?这将显示一个包含变量的消息框。
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签