在Drupal 6中使用jQuery表单提交

时间:2012-08-07 15:16:18

标签: jquery forms drupal drupal-6 drupal-modules

我正在使用Drupal 6,我有一个非常简单的表单,我只在自定义模块中创建了三个字段。我还将表单作为块放在左侧导航中。这是一个搜索表单。它有两个选择框和一个自由格式文本字段。目标是能够从字段中获取值并使用jQuery进行AJAX调用,执行搜索并使用结果将JSON返回到前端并使jQuery循环并显示结果。我在Drupal工作流程中遇到了困难,特别是因为表单处于阻塞状态。那么在我的hook_form_submit中如何正确处理jQuery调用?理想情况下,我希望它可以在任何页面上,如果他们填写左侧边栏中的表单并提交它,他们将被移植到处理jQuery的“结果”页面,但我无法弄清楚适当的流程如何/何时/何处添加JavaScript / jQuery以正确处理此问题。如果我在我的hook_form_submit中并尝试处理表单,我如何将结果发送到主要内容区域的结果页面?

感谢

1 个答案:

答案 0 :(得分:0)

我觉得你们这里有相互矛盾的要求。您希望使用ajax加载表单的结果,但您还希望将用户重定向到结果页面。

如果您要将用户重定向到另一个页面,只需在视图中创建搜索表单,然后在表单提交中执行drupal_goto('search-page')。由于你的页面加载ajax是不必要的。

如果你想在不加载页面的情况下加载结果,这就是ajax派上用场的地方。然后我会用ajax处理程序完全跳过drupal。不添加按钮类型只提交没有处理程序的按钮。然后在ajax文件中将行为附加到按下按钮。

ajax应该为这个ajax回调调用一些独特的url,比如'ajax / search-form'。 在您的hook_menu中注册此网址,如

$items['ajax/search-form/%/%/%'] = array(
  'title' => 'my title'
  'page callback' => 'my_module_ajax_search'
  'page arguments' => array(2,3,4),
);
return $items;

然后在你的函数中:

function my_module_ajax_search($first_param, $second, $third) {
  // Generate output and return it to the calling ajax function through print.
  $output = '<div>my output</div>'
  print $output;

  // Make sure Drupal closes out then bails so it doesn't output another
  // page wrapper.
  module_invoke_all('exit'); // Called automatically in Drupal 7 but not 6.
  exit(); 
}

您的Ajax看起来像:

$('.button').click(function(
  value1 = $('.box1').value() // dont remember the actual function to extract a text field value.
  value2 = ...
  $.ajax({
    url: Drupal.settings.basePath + 'ajax/search-form/' + value1 + '/' + value2 + '/' + value3,
    success: function(data) {
      $('.my content').html(data);
    }
  });
)