Take the 2-minute tour ×

I have a big problem, I need to save an AJAXed ordered list as a menu, I found a script here: http://www.prodevtips.com/2010/03/07/jquery-drag-and-drop-to-sort-tree/ it works great, except that I need to save it to my database and the author says you can do it with JSON. Here is the part he says can process and save the data.

        var tree = $.toJSON(parseTree($("#tag_tree")));
        $.post("@saveTags", {tags: tree}, function(res){
        $.debug.print_r(parseTree($("#tag_tree")), "printOut", false);

the debug outputs an array like :

'0' => "Economics"
    '1' => "Blogging"
    '2' => "General Dev"
    '3' => "Japan"
    '4' ...
        '0' => "Productivity"
        '1' ...
            '0' ...
                '0' => "Humanities"
    '3' => "CMS"  

What I need is to be able to save the id, parent id and the order. It seems that i'm not able to save them into the database.

I know i need a php file to save the data to the database but i don't know how to pass the data to that php file. This is with what i need help, how can i pass the data to a php file in this case?

Thanks, Dan

share|improve this question
That's what he said. AJAX - asynchronous javascript –  d-_-b Apr 11 '11 at 12:33

1 Answer 1

up vote 3 down vote accepted

With jQuery AJAX calls are made very simple. Let's take a look at the jQuery script.

    url: "file.php",
    type: 'POST',
    data: {var1: value1, var2: value2},
    dataType: 'json',
    success: function() {}
    error: function() {}

Where "file.php" is the name of the PHP file that will process data. 'POST' being the type of method used to send data (GET is just a little bit easier to hack, but a bit faster to use. You have to decide it yourself). Then data can be everything. You are suggested to use JSON as data type, then you sould be able to do that easily setting a thing such as var data = {var1: value1, var2: value2} and then replace data: {var1: value1, var2: value2} with data: data.

Then inside the file.php you can recover your JSON strings with: $json = $_POST['data'] and convert it with json_decode($json, true) into an associative array.

If you encounter any problems running the Javascript script, you should take a look at the debug functions provided by Firebug.


share|improve this answer

Your Answer


