将jquery json数据保存到数据库中
我有一个大问题,我需要将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 jQuery AJAX 调用变得非常简单。让我们看一下 jQuery 脚本。
其中“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.
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 replacedata: {var1: value1, var2: value2}
withdata: data
.Then inside the file.php you can recover your JSON strings with:
$json = $_POST['data']
and convert it withjson_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: