dow57588 2011-04-11 12:09
浏览 64
已采纳

将jquery json数据保存在数据库中

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.

$("#save").click(function(){
        /*
        var tree = $.toJSON(parseTree($("#tag_tree")));
        $.post("@saveTags", {tags: tree}, function(res){
            $("#printOut").html(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

  • 写回答

1条回答 默认 最新

  • douchao9899 2011-04-11 12:34
    关注

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

    $.ajax({
        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.

    References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)