php - 使用 ajax jquery 和 php 将 sortable 保存到 mysql

标签 php jquery mysql ajax jquery-ui

我有一个包含多个拖放框的页面,效果很好,但不起作用的是框中的链接。如果有人可以帮助我,我将不胜感激:)。所以我有一个页面,人们可以在其中拖放框(它工作正常,正如我之前所说),框内的链接也可以排序,但我似乎无法让它们将值保存到 mysql。我认为这两个拖放之间存在冲突,也许我做错了,因为我以前没有使用过 ajax 和 jquery。

//here is the jquery where I need to add some ajax
$(function() {
$('.dragbox-content').sortable({ 
  connectWith: '.dragbox-content',
  update: function(event, ui) {
        var order=$(this).attr('id');
        alert(order);  // I get the order alert and it has one value that I need, but I need the sort order aswell                         
  }
  });                 
});

//this is the <div> that has the links in them and mysql query that gets the values
//from two different databases, one is for the boxes and the other for links.
//boxes db id = links title_id
echo '<div class="dragbox-content" id="order'.$widget['id'].'"';'>''</div>';

$sql10 = "SELECT u.*, w.id, w.link_address, w.link_name FROM db_boxes u LEFT 
JOIN db_links w ON u.link_id = w.id  WHERE 
(u.username = '$username' AND u.link_id !='0' AND w.title_id = '".$widget['id']."' 
AND w.link_name !='pilt' AND w.rights = '0') OR 
(u.username = '$username' AND u.link_id !='0' AND w.title_id = '".$widget['id']."' 
AND w.link_name !='pilt' AND w.rights LIKE '%26%') ORDER BY w.id ASC";
      $result10 = mysql_query($sql10) or die (mysql_error());
      while ($row = mysql_fetch_array($result10)) {
        $link_id = $row['id'];
        $link_address = $row['link_address'];
        $link_name = $row['link_name'];
        $title_id = $row['title_id'];
      ?>
      <div class="move" id="<?php echo $link_id;?>">
      <span class="link_style">
      <div><a href="<?php echo $link_address; ?>"><?php echo $link_name;?>     </a></div</span></div>

我只需要有人告诉我如何在用户每次点击该页面时使用 ajax 将 tile_id 和 sort_order 保存到 boxes 数据库

最佳答案

请参阅下面的示例:

http://jsfiddle.net/gRoberts/vMy7r/

$(function () {
    $('ul').sortable({
        update : function(e, ui) {
            var ul = $(ui.item).closest('ul');
            var index = 0;
            var toPost = {};
            ul.find('> li').each(function() {
                index++;
                $(this).find('input').val(index);               
                toPost[$(this).find('input').attr('name')] = index;
            });                  
            $.ajax({
                url : '/echo/json/',
                data : toPost,
                type : 'POST',
                dataType : 'json',
                success : function(resp) {
                    alert(resp);
                },
                error : function() {
                    alert('There was a problem');
                }
            });
        }
    });
});
​

上面的例子可以有两种使用方式,如果你删除 $.ajax 它将更新隐藏的表单字段,然后你可以正常发布。

关于php - 使用 ajax jquery 和 php 将 sortable 保存到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11682641/

相关文章:

php - 服务器检测

jquery - 如何使用 Jquery Datatable 仅对当前页面进行排序

mysql - 如何在局域网服务器上找到mysql数据库...?

PHP/AndroidStudio - 通过让当前用户作为外键登录来发帖

php - Ajax 关联数组

javascript - yii2 PJAX 如何在 pjax 重新加载后显示模态?

javascript - 使用jquery在表中自动计算

php - 警告:mysqli::reap_async_query():连接未打开、清除或已关闭

mysql - 根据字符串匹配插入行

php - 将数据库与外部表连接