Drupal表单提交



我在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;

}

您可以编写以下提交函数,

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
}

这很简单。你只需要实现form_submit函数。从上面的函数中,我假设您的模块名称为q_search。那么,submit函数应该是这样的:

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
}

如果验证失败,可以在validate函数中使用form_set_error

您可以在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).

最新更新