从表单 API 复选框获取值

发布于 2024-09-12 03:21:58 字数 2940 浏览 4 评论 0原文

这是我正在开发的一个模块,用于创建自定义过滤搜索。但我不知道如何获取表单类型复选框的值...我搜索过,但什么也没有!

<?php

function my_module_menu() {
  $items = array();
  $items['my_module/form'] = array(
    'title' => t('My form'),
    'page callback' => 'my_module_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function my_module_form() {
  return drupal_get_form('my_module_my_form');
}

function my_module_my_form($form_state) {
  $form['name'] = array(
    '#type' => 'fieldset',
    '#title' => t('Search'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // Removes the #required property and
  // uses the validation function instead.
  $form['name']['first'] = array(
    '#type' => 'textfield',
    '#title' => t('Search'),
    '#default_value' => "Keyword",
    '#description' => "Please enter your keyword.",
    '#size' => 20,
    '#maxlength' => 20,
  );
 $form['name']['filter'] = array(
    '#type' => 'fieldset',
    '#title' => t('Filter'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
   $form['name']['filter']['node_options'] = array(
   '#type' => 'checkboxes',
   '#title' => t('Default options'),
   '#default_value'   => variable_get('node_options', 0),
   '#options' => array(
  '31' => t('Chinese'),
  '28' => t('South Indian'),
  '18' => t('Pizza'),

   ),
   '#description' => t('Filter the results.'),
 );

  $form['name']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  // Adds a new button to clear the form. The #validate property
  // directs the form to use a new validation handler function in place
  // of the default.
 /* $form['clear'] = array(
    '#type' => 'submit',
    '#value' => 'Reset form',
    '#validate' => array('my_module_my_form_clear'),
  );*/

  return $form;
}

// This is the new validation handler for our Reset button. Setting
// the $form_state['rebuild'] value to TRUE, clears the form and also
// skips the submit handler.
function my_module_my_form_clear($form, &$form_state) {
    $form_state['rebuild'] = TRUE;
}


//block
function my_module_block($op = 'list', $delta = 0, $edit = array()) {
  $block = array();

  switch ($op) {
    case 'list':
      $block[0]['info'] = t('Custom search form');
      break;
    case 'view':
      switch ($delta) {
        case 0:
          $block['subject'] = t('Custom search');
          $block['content'] = drupal_get_form('my_module_my_form');
          break;
      }
      break;
  }

  return $block;
}










function my_module_my_form_submit($form, &$form_state) {
  $redirect_url = 'search/node/';
  $redirect_url .= ' category:' . $form_state['values']['filters'];
  $redirect_url .= ' %' . $form_state['values']['first'] . '%';

  $form_state['redirect'] = $redirect_url;

}

This is a module that I'm working on to create a custom filtered search. But i have no idea on getting the values of form type checkboxes... I searched but nothing yet!

<?php

function my_module_menu() {
  $items = array();
  $items['my_module/form'] = array(
    'title' => t('My form'),
    'page callback' => 'my_module_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function my_module_form() {
  return drupal_get_form('my_module_my_form');
}

function my_module_my_form($form_state) {
  $form['name'] = array(
    '#type' => 'fieldset',
    '#title' => t('Search'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );

  // Removes the #required property and
  // uses the validation function instead.
  $form['name']['first'] = array(
    '#type' => 'textfield',
    '#title' => t('Search'),
    '#default_value' => "Keyword",
    '#description' => "Please enter your keyword.",
    '#size' => 20,
    '#maxlength' => 20,
  );
 $form['name']['filter'] = array(
    '#type' => 'fieldset',
    '#title' => t('Filter'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
   $form['name']['filter']['node_options'] = array(
   '#type' => 'checkboxes',
   '#title' => t('Default options'),
   '#default_value'   => variable_get('node_options', 0),
   '#options' => array(
  '31' => t('Chinese'),
  '28' => t('South Indian'),
  '18' => t('Pizza'),

   ),
   '#description' => t('Filter the results.'),
 );

  $form['name']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  // Adds a new button to clear the form. The #validate property
  // directs the form to use a new validation handler function in place
  // of the default.
 /* $form['clear'] = array(
    '#type' => 'submit',
    '#value' => 'Reset form',
    '#validate' => array('my_module_my_form_clear'),
  );*/

  return $form;
}

// This is the new validation handler for our Reset button. Setting
// the $form_state['rebuild'] value to TRUE, clears the form and also
// skips the submit handler.
function my_module_my_form_clear($form, &$form_state) {
    $form_state['rebuild'] = TRUE;
}


//block
function my_module_block($op = 'list', $delta = 0, $edit = array()) {
  $block = array();

  switch ($op) {
    case 'list':
      $block[0]['info'] = t('Custom search form');
      break;
    case 'view':
      switch ($delta) {
        case 0:
          $block['subject'] = t('Custom search');
          $block['content'] = drupal_get_form('my_module_my_form');
          break;
      }
      break;
  }

  return $block;
}










function my_module_my_form_submit($form, &$form_state) {
  $redirect_url = 'search/node/';
  $redirect_url .= ' category:' . $form_state['values']['filters'];
  $redirect_url .= ' %' . $form_state['values']['first'] . '%';

  $form_state['redirect'] = $redirect_url;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2024-09-19 03:21:58

如果您尝试获取表单构建器中包含在 $form['name']['filter']['node_options'] 中的表单字段的值,则在提交处理程序中您需要使用$form_state['values']['node_options']

此外,菜单回调应更改为

$items = array();
$items['my_module/form'] = array(
  'title' => t('My form'),
  'page callback' => 'drupal_get_form',
  'page arguments' => array('my_module_form'),
  'access arguments' => array('access content'),
  'description' => t('My form'),
  'type' => MENU_CALLBACK,
);

无需定义两个函数(第一个函数调用第二个函数)来定义表单生成器。

If you are trying to get the value for the form field that in the form builder is contained in $form['name']['filter']['node_options'], then in the submission handler you need to use $form_state['values']['node_options'].

Also the menu callback should be changed to

$items = array();
$items['my_module/form'] = array(
  'title' => t('My form'),
  'page callback' => 'drupal_get_form',
  'page arguments' => array('my_module_form'),
  'access arguments' => array('access content'),
  'description' => t('My form'),
  'type' => MENU_CALLBACK,
);

There is no need to define two functions, where the first call the second one, to define a form builder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文