在数据库中保存jquery json数据

时间:2021-07-13 07:41:08

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.

我有一个大问题,我需要保存一个AJAXed有序的列表作为一个菜单,我在这里找到了一个脚本:http://www.prodevtips.com/2010/03/07/jquery-drag- drop-to- 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);
    }); 

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.

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

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?

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

Thanks, Dan

谢谢你,丹

1 个解决方案

#1


3  

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

使用jQuery AJAX,调用非常简单。让我们来看看jQuery脚本。

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

”文件。php“是将处理数据的php文件的名称。POST是一种用来发送数据的方法(GET稍微容易一些,但是使用起来更快一些)。你必须自己决定)。那么数据就是一切。建议您使用JSON作为数据类型,然后您应该能够轻松地设置诸如var数据= {var1: value1, var2: value2},然后用data: data替换data: {var1: value1, var2: value2}的数据。

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.

然后在文件。php可以使用:$ JSON = $_POST['data']恢复JSON字符串,并将其与json_decode($ JSON, true)转换为关联数组。

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

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

References:

引用:

#1


3  

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

使用jQuery AJAX,调用非常简单。让我们来看看jQuery脚本。

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

”文件。php“是将处理数据的php文件的名称。POST是一种用来发送数据的方法(GET稍微容易一些,但是使用起来更快一些)。你必须自己决定)。那么数据就是一切。建议您使用JSON作为数据类型,然后您应该能够轻松地设置诸如var数据= {var1: value1, var2: value2},然后用data: data替换data: {var1: value1, var2: value2}的数据。

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.

然后在文件。php可以使用:$ JSON = $_POST['data']恢复JSON字符串,并将其与json_decode($ JSON, true)转换为关联数组。

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

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

References:

引用: