2

I am working on a jQuery sortable. I pull information from my database to populate each orderable element and then repost the new order back to my data base. However I cannot figure out how to take the variable that I define in my serialize jQuery and enter it into my AJAX function in the data: area.

Here is my code:

$(document).ready(function() 
{              
    $('#social_list').sortable(
    { 
        update: function() 
        {
            var order = $('#social_list').sortable('serialize');
            alert(order);
        }                         
    });
});

$.ajax(
{
    url: 'save_items_order.php',
    data: ?,
    type: '$_POST', 
    success: function(msg)
    {
        alert(msg);
    }
});

So I want to take the string that var order creates and pass it into the ajax so data: = var order string

I tried data: $('#social_list').sortable('serialize'), everything seemed to work it just doesn't seem to update my table

2 Answers 2

2

Simplest solution is to make a global

$(document).ready(function() {
    $('#social_list').sortable({
        update: function() {
            // global. you could just omit var, but I find this clearer
            window.order = $('#social_list').sortable('serialize');
        }
    });
});

$.ajax({
    url: 'save_items_order.php',
    data: window.order, /* ...rest of code */
});

But I'm sure you know globals are bad, you can at least contain them to your namespace,

var myNs = {};
$(document).ready(function() {
    $('#social_list').sortable({
        update: function() {
            myNs.order = $('#social_list').sortable('serialize');
        }
    });
});

$.ajax({
    url: 'save_items_order.php',
    data: myNs.order /* ... rest of code */
});

or re-organize your code so they can share variables.

$(document).ready(function() {
    var order;
    $('#social_list').sortable({
        update: function() {
            order = $('#social_list').sortable('serialize');
        }
    });
    $('#myButton').click(function(){
        $.ajax({
            url: 'save_items_order.php',
            data: order, /* ... rest of code */
        });
    });
});

Note that for this case, you could just do the following

$.ajax({
    url: 'save_items_order.php',
    data: $('#social_list').sortable('serialize') /* ... rest of code */
});
Sign up to request clarification or add additional context in comments.

3 Comments

... the ajax request will be sent long before the variable is set.
@KevinB Well, that's not what the question is about. If the user hasn't changed the sort order, OP needs to give it a default. I don't think the OP is making the AJAX call as soon as the page is loaded. Do I need a warning "above code is just an example?" The real question is about variable scope
@JuanMendes Super Globals are way to messy for me but the answer of setting data: $('#social_list').sortable('serialize') seems to work. (The only reason I say seems to is I think I have an error on my php page)
-1

You just pass any javascript object in as the data.

For your specific case, I would probably wrap the AJAX call in a function, like:

function saveItemOrder(order)
{
    $.ajax({
        url: 'save_items_order.php',
        data: { "order": order },
        type: 'POST', // <-- this should be just "POST"
        success: function(msg){
            alert(msg);
        }
    });
}

And then:

update: function() {
    var order = $('#social_list').sortable('serialize');
    // alert(order);

    saveItemOrder(order);
}  

1 Comment

That seemed to break my sortable action... I tried data: $('#social_list').sortable('serialize'), everything seemed to work it just doesn't seem to update my table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.