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

发布于 2024-10-31 07:30:06 字数 1042 浏览 0 评论 0原文

我有一个大问题,我需要将 AJAXed 有序列表保存为菜单,我在这里找到了一个脚本: http://www.prodevtips.com/2010/03/07/jquery-drag-and-drop-to-sort-tree/它工作得很好,除了我需要将它保存到我的数据库中,而且作者说你可以使用 JSON 来完成。他说这是可以处理和保存数据的部分。

$("#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);
    }); 

调试输出一个数组,如下所示:

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

我需要的是能够保存 id、父 id 和订单。看来我无法将它们保存到数据库中。

我知道我需要一个 php 文件来将数据保存到数据库,但我不知道如何将数据传递到该 php 文件。这是我需要帮助的,在这种情况下我如何将数据传递到 php 文件?

谢谢, 担

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零時差 2024-11-07 07:30:06

使用 jQuery AJAX 调用变得非常简单。让我们看一下 jQuery 脚本。

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

其中“file.php”是将处理数据的 PHP 文件的名称。 “POST”是用于发送数据的方法类型(GET 更容易破解,但使用起来更快。您必须自己决定)。那么数据就可以是一切。建议您使用 JSON 作为数据类型,这样您就可以轻松设置诸如 var data = {var1: value1, var2: value2} 之类的内容,然后替换 data : {var1: value1, var2: value2}data: data

然后在 file.php 中,您可以使用以下命令恢复 JSON 字符串: $json = $_POST['data'] 并使用 json_decode($json, true) 将其转换为关联数组。

如果您在运行Javascript脚本时遇到任何问题,您应该查看Firebug提供的调试功能。

参考资料:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文