简体   繁体   English

在Drupal 6中使用jQuery提交表单

[英]Form submission with jQuery in Drupal 6

I am using Drupal 6 and I have a very simple form with only three fields that I created in a custom module. 我正在使用Drupal 6,我有一个非常简单的表单,其中只有三个字段是在自定义模块中创建的。 I also placed the form as a block in the left nav. 我还将该表格作为块放置在左侧导航栏中。 This is a search form. 这是一个搜索表。 It has two select boxes and one free form text field. 它具有两个选择框和一个自由格式文本字段。 The goal is to be able to take the values from the fields and use jQuery to make an AJAX call, perform the search and return JSON to the front end with the results and have jQuery loop through and display the results. 目的是能够从字段中获取值,并使用jQuery进行AJAX调用,执行搜索并将JSON返回结果的前端,并使jQuery循环并显示结果。 I'm really having difficulties following the Drupal work flow though, especially since the form is in a block. 不过,在遵循Drupal的工作流程时,我确实遇到了困难,尤其是因为表格很困难。 So how in my hook_form_submit do I handle the jQuery call properly? 那么如何在我的hook_form_submit中正确处理jQuery调用呢? Ideally, I would like it to where whatever page they are on, if they fill out the form in the left sidebar and submit it, they get ported to a "results" page that handles the jQuery, but I just can't figure out the proper flow of how/when/where to add the JavaScript/jQuery to handle this correctly. 理想情况下,我会把它想在那里任何网页,他们都在,如果他们填写在左侧边栏的表格,并提交,他们得到移植到一个“结果”页面,处理jQuery的,但我就是想不通如何/何时/在何处添加JavaScript / jQuery以正确处理此问题的正确流程。 If I am in my hook_form_submit and attempt to process the form, how would I get the results to a results page in the main content area? 如果我在hook_form_submit中并尝试处理表单,如何将结果获取到主要内容区域中的结果页面?

THANKS 谢谢

I feel like you have conflicting requirements here. 我觉得您的要求有冲突。 You want to load the results of the form with ajax but you also want to redirect the user to a results page. 您想使用ajax加载表单的结果,但您也想将用户重定向到结果页面。

If your going to redirect the user to another page just create the search form in views then in the form submit do a drupal_goto('search-page'). 如果您要将用户重定向到另一个页面,只需在视图中创建搜索表单,然后在表单提交中执行drupal_goto('search-page')。 Since your doing a page load ajax is unnecessary. 由于您无需加载页面,所以无需使用ajax。

If you want to load the results without doing a page load this is where ajax comes in handy. 如果您要加载结果而不执行页面加载,这就是ajax派上用场的地方。 Then I would just skip drupal altogether with the ajax handler. 然后,我将与ajax处理程序一起完全跳过drupal。 do not add a button type submit just a button without a handler. 不要添加按钮类型,只需提交没有处理程序的按钮即可。 Then in an ajax file attach a behavior to the button press. 然后在ajax文件中将行为附加到按钮按下。

The ajax should call some unique url for this ajax callback like 'ajax/search-form'. 该ajax应该为此ajax回调调用一些唯一的URL,例如“ ajax / search-form”。 Register this url in your hook_menu like 在您的hook_menu中注册该网址,例如

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

Then in your function: 然后在您的函数中:

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(); 
}

Your Ajax would look something like: 您的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);
    }
  });
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM