由于前几天一直苦于JQuery跟Drupal后台的数据交互的处理,翻阅了很多的文档,但是都说的不是很全,现在我把具体的思路跟大家分享一下,好让别的新用JQuery的同志跟我一下,差点搞死,呵呵
具体的是要用到几个关键的函数:
drupal_to_js:这个后台把数据按照JSON形式组织返回给Jquey的前台,在JQuery的脚本里面就直接用Drupal.parseJSon(data)得到,得到的是一个数组,是Javascript的数组。你可以把这个加到你的页面里面。
$.post()(当然也有$.get()方法)$.post(url(你要请求的路径:这里要注意一下,要想到后台就要通过这个路径了,大家知道在menu里面有一个callback 也就是这个路径后可以对应的调用一个函数 这个函数就是你后台要处理数据的地方,在这个函数里面把后台的值用drupal_to_js()传就可以了, 你就可以随便在你自己定义的一个module里面定义这个函数,然后就把你的URL设为Menu下面的path,就ok了。))在$.post(url,parameters(注:键值对),callbackfucntion)。在后台的函数里面就直接用$_post 或数$_GET函数就可以得到了。
这是页面的代码:
<?php
drupal_add_js(
'$(document).ready(function(){
$("#fathersort").change( function() {
var callfunction=
function(data){
$("#childsort").empty();
var result = Drupal.parseJson(data);//解析服务器端传来的数据
for(key in result) $("#childsort").append("<option value="+result[key]+">"+result[key]+" </option>");
};//跟新页面的内容
$.post("?q=node/add/productpage1", //这个路径跟menu的一致 {hello:$("#fathersort")[0].value},callfunction );//这里是要传的参数
});
});',
'inline'
);
?>
服务器端:function productpage1_menu($may_cache) {
$items = array();
// Do not cache this menu item during the development of this module.
if (!$may_cache) {
$items[] = array(
'path' => 'node/add/productpage1',
'title' => t('productpage1'),
'access' => true,
'callback' => 'getchildsorts_by_parenetsort',//这里就是页面跟后台的连接的地方,path跟
);//url一致 用callback调用后台的处理函数getchildsorts_by_parenetsort,
}
return $items;
}
function getchildsorts_by_parenetsort(){//这个函数得到页面的传值并且把要传给页面的数据传回去
$options=array();
$parentsort=$_POST['hello'];//得到页面传值
$result=db_query("select * from {sort} where parentName='$parentsort'") ;
$i=0;
while($data=db_fetch_object($result)){
$options[$i]=$data->sortName;
$i++;
}
print drupal_to_js($options);//把数据返回给页面
exit;
}
希望大家能看的懂了 呵呵,欢迎来我们网站:www.solobigfish.com互动。
我现在的水平还看不
我现在的水平还看不懂,呵呵!
支持一下,感谢你的分享
--------------AD-----------------
新转drupal5.x站 http://www.zbcpet.com
哈哈,慢慢看,我也
哈哈,慢慢看,我也正在学.
__________________________
我要减肥螺旋藻
不错的文章,不知道
不错的文章,不知道楼主研究过Json_service没?
又是一片强大的文章
又是一片强大的文章,纯支持一下,等我到达这个水平了再来研究:)
得到一些启发
你好!我是一个drupal的新手,之前用drupal做过一些简单的表单和模块,在此之前主要是做基于JQuery的Ajax开发的(主要也是用JSON),用php但不用任何框架,写起来比较自由,但现在在drupal上开发遇到了困难,不知道你能不能给我一些帮助。
我遇到的问题:
1. 在drupal的FormAPI中,我们知道,在表单提交之后数据库中可能会有一些级联的操作,当我们用Ajax提交表单时如何处理这部分?
2. 能不能提供一部分代码或者给我解释一下在drupal中表单提交的整个流程呢?
3. hook_form_submit是不是在表单提交之后自动调用的呢?
http://drupal.org/node/288974 这是我在drupal官网上发的一个帖子但没有得到答案,希望你能帮忙,谢谢!
function productpage1_menu($may_cache) {
$items = array();
// Do not cache this menu item during the development of this module.
if (!$may_cache) {
$items[] = array(
'path' => 'node/add/productpage1',
'title' => t('productpage1'),
'access' => true,
'callback' => 'getchildsorts_by_parenetsort',//这里就是页面跟后台的连接的地方,path跟
);//url一致 用callback调用后台的处理函数getchildsorts_by_parenetsort,
}
return $items;
}
为什么你不缓存这个菜单?
drupal表单提交后首先
drupal表单提交后首先用hook_validate来校验,校验通过后才调用hook_submit
如果要Ajax提交表单,些许可以考虑用ahah?Drupal的form_api帮助里有介绍
------------------------------
www.ykfan.cn
很强大的帖子 ----------
很强大的帖子
----------------------------------------
学习Drupal中
http://www.722k.com
----------------------------------------