表单使用#ahah增强ajax体验

-- Submitted by Admin on Thu, 05/13/2010 - 19:25

Drupal为我们提供了表单的专用方法#ahah,所以只是将js里的ajax调用代码换了一种形式,更方便。

步骤:

一、表单中定义#ahah属性

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => 'SEARCH',
  '#attributes' => array('class'=>'class-search'),
  '#ahah' => array(
    'event' => 'click',//定义触发事件类型
    'method' => 'replace',//替换wrapper指向的容器里的内容,可以改成其它方式
    'path' => 'class_search_results',//ahah调用的菜单路径
    'wrapper' =>'content',//返回的内容将以method定义的方式来改变指定容器内容
    'effect' => 'fade',//以渐现形式来显示返回的内容
    'progress'=>array(
      'type' => 'throbber',//会显示drupal默认的小时钟图标以表明是正在异步获取数据
    ),
  ),
);

二、定义一个#ahah要调用的路径

$items['class_search_results'] = array(
  'title' => t('Search Results'),
  'page callback' => 'class_search_results',
  'access callback' => TRUE,
  'type' => MENU_CALLBACK,
);

三、定义调用路径的回调函数

function class_search_results() {
  $output = 'hello, ahah!';
  drupal_json(array('status' => TRUE, 'data' => $output));
}

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <code> <cite> <ul> <ol> <li> <dl> <dt> <dd> <img> <ebmed>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
9 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.