Drupal表单提交

时间:2013-05-24 09:43:12

标签: drupal drupal-forms

我在drupal中创建了一个表单。我不知道如何处理提交。我想从数据库中选择一些来自表单的值。这是我创建表单的代码

    function q_search_form() {
$form['qsearch']['category'] = array(
    '#type' => 'select',
    '#options' => array(0 => 'Any', 1 => 'Automotive', 2 => 'Real Estate'),
    '#attributes' => array('class' => 'drop-box'),
    '#prefix' => '<table width="470"><tr><td width="170">Select Category</td><td width="300">',
    '#suffix' => '</td></tr>'
);
$form['qsearch']['city'] = array(
    '#type' => 'select',
    '#options' => array(0 => 'Any', 1 => 'Calicut', 2 => 'Kochi'),
    '#attributes' => array('class' => 'drop-box'),
    '#prefix' => '<tr><td width="170">City</td><td width="300">',
    '#suffix' => '</td></tr>'
);
$form['qsearch']['property'] = array(
    '#type' => 'select',
    '#options' => array(0 => 'Any', 1 => 'House', 2 => 'Land'),
    '#attributes' => array('class' => 'drop-box'),
    '#prefix' => '<tr><td width="170">Property</td><td width="300">',
    '#suffix' => '</td></tr>'
);
$form['qsearch']['wanto'] = array(
    '#type' => 'select',
    '#options' => array(0 => 'Any', 1 => 'Sell', 2 => 'Buy'),
    '#attributes' => array('class' => 'drop-box'),
    '#prefix' => '<tr><td width="170">Want to</td><td width="300">',
    '#suffix' => '</td></tr>'
);
$form['qsearch']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
    '#attributes' => array('id' => 'Search', 'class' => 'srch-button'),
    '#prefix' => '<tr><td><a class="adv-srch" href="#">Advance Search</a></td><td>',
    '#suffix' => '</td></tr></table>'
);
return $form;

}

3 个答案:

答案 0 :(得分:0)

这很简单。您只需要实现form_submit功能。从上面的函数中,我假设您的模块名称为q_search。所以,这是提交函数的样子:

function q_search_form_submit($form, &$form_state) {
  // you can get the values submitted by the users using
  // `$form_state['values']`, and use the database functions
  // to implement your logic.
}

如果您还想在实际提交之前验证用户输入,则应添加一个看似如下的验证函数:

function q_search_form_validate($form, &$form_state) {
  // validate here
}

如果验证失败,您可以在验证功能中使用form_set_error

答案 1 :(得分:0)

您可以编写以下提交功能,

function q_search_form_submit($form, &$form_state) {
  // To get selected values from form
  $values = $form_state['values'];
  // do print_r($values); exit; to check the values
}

答案 2 :(得分:0)

您可以在form_submit函数中执行任何您想要的操作。

function q_search_form_submit($form, &$form_state) {
  $category = $form_state['values']['category'];
  $city = $form_state['values']['city'];
  //etc..

  //use these values to query your database tables
  $query = db_select($category, 'c');
  //narrow results to those only in the selected city:
  $query->join($city, 'cy', 'c.city = cy.cid');
  //narrow results further with more ->join statements
  $results = $query
    ->fields('c', array(fields you want from the categories table))
    //fields from other tables...
    ->execute();

  //do whatever with the $results (print on a different page, print in a table, etc).