Jump to content

Updating database with php/ajax/jquery


Aur1cles

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.