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(""); };//跟新页面的内容 $.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;
}

No votes yet

评论

发表新评论

CAPTCHA
请补全“__”处缺失的字,你懂的!
请填写一个正确的答案