Jump to content

Recommended Posts

I have sortable panels, that users can drag around, that part works perfect. Inside those panels I have Jquery sortable links, that users can drag around the panels and I can't seem to get this to update database.

This is the part where it get's the link paths from database and shows them:

<ul style="list-style-type: none;" class="drag" id="order_<?php echo $row['id'];?>" name="<?php echo $link_sort_no;?>" title="<?php echo $title_id;?>"><li>
          <a href="<?php echo $link_adress; ?>"><?php echo $link_name;?></a><br/><br/>
          </li></ul>

This is my Jquery and ajax:

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

 

And this is UpdateLink.php file, what should update the database:

$order = $_POST['order'];
for ($i = 0; $i < count($order); $i++) {
  mysql_query("UPDATE links SET link_sort_no=" . $i . " WHERE id=' ".$order[$i]."'") or die(mysql_error());

 

But I can't seem to get this to work, I' been trying everything. Maybe there's something wrong with ajax or jquery, I really don't know. I am just asking somebody to take a look at it and maybe helping a little bit  :) I would appreciate it.

I forgot to mention that users can drag links from one panel to another, so it needs to save two values to database title_id and link_sort_no.

Link to comment
https://forums.phpfreaks.com/topic/266730-updating-database-with-phpajaxjquery/
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.