0

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

}

4

3 回答 3

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).
于 2013-07-23T18:24:51.167 回答
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
}

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

于 2013-05-24T10:03:37.970 回答
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
}
于 2013-05-24T10:05:24.097 回答