2011-03-18 51 views
0

我想提高我在模塊開發的知識(這是遠離基本的),所以我試着開發周邊搜索模塊。 我已經取得了什麼現在包含表單塊:

function perimeter_search_block_view($delta = '') { 
    // Define an empty array for the block output. 
    $block = array(); 

    switch($delta) { 
    case 'perimeter_search_box': 
     $block['subject'] = t('Perimeter search box'); 
     $block['content'] = drupal_get_form('perimeter_search_form');; 
     break; 
    } 

    return $block; 
} 

/** 
* Implementation of the perimeter search form 
* @return array with form data 
*/ 
function perimeter_search_form($form, &$form_state) { 
    $form = array(
    '#action' => 'perimeter-search-results', 
    'keyword' => array(
     '#type' => 'textfield' 
    ), 
    'location' => array(
     '#type' => 'textfield' 
    ), 
    'perimeter' => array(
     '#type' => 'select', 
     '#title' => t('Perimeter'), 
     '#options' => array('15 km', '30 km', '60 km', '120 km') 
    ), 
    'submit' => array(
     '#type' => 'submit', 
     '#value' => t('Start search') 
    ) 
); 

    return $form; 
} 

我也有一個函數來輸出搜索結果:

/** 
* Implementation of hook_menu() 
* @return defined menu/page items 
*/ 
function perimeter_search_menu() { 
    $items = array(); 

    // Search results page 
    $items['perimeter-search-results'] = array(
    'title' => t('Perimeter search results'), 
    'page callback' => 'perimeter_search_results', 
    'access arguments' => array('view perimeter search'), 
    'type' => MENU_NORMAL_ITEM 
); 

    return $items; 
} 

/** 
* Processing job search queries 
*/ 
function perimeter_search_results() { 
    $page_content = t('Search results'); 
    return $page_content; 
} 

我的(?簡單)的問題是:如何在我perimeter_search_results()函數來獲取數據後(關鍵詞,位置,周長)?

回答

0

容易,你必須創建_submit功能爲您的形式,在這裏一個例子:

function perimeter_search_form_submit($form, &$form_state) { 
    /* 
    * Your data handling goes here on the $form_state['values']['myfieldname'] 
    * variable. 
    */ 
    drupal_set_message(t('Awesome, you managed to fill the form!')); 
} 

如果你需要驗證..

function perimeter_search_form_validate($form, &$form_state) { 
    if($form_state['values'['myfieldname'] == '') { 
     form_set_error('', t('Hey, it doesn't work like that!')); 
    } 
} 

只要記住,如果添加屬性「#REQUIRED」 => TRUE到表單字段,字段將被自動驗證,總是要求現場,所以你不需要使用驗證該字段,如果你只需要它被編譯。