2014-09-04 85 views
0

我已經在Drupal中使用dupal API爲表單創建了自定義模塊,並在我的數據庫中創建了一個表格 我想要的是將下拉框的值而不是索引保存到數據庫中...我.module文件是:在Drupal表單提交

<?php 
function form_test_menu() { 
$items['formtest'] = array(
    'title' => 'Form Test', 
    'page callback' => 'drupal_get_form', 
    'page arguments' => array('form_test_form'), 
    'access callback' => TRUE, 
); 
return $items; 

}

function form_test_form($form,&$form_submit) { 
$form['name'] = array(
    '#title' => t('Name'), 
    '#type' => 'textfield', 
    '#required' => TRUE, 
); 
$form['college'] = array(
    '#title' => t('College/University'), 
    '#type' => 'textfield', 
    '#required' => TRUE, 
); 
$form['education'] = array(
    '#title' => t('Education'), 
    '#type' => 'select', 
    '#description' => 'Select your higher education .', 
    '#options' => array(t('--- SELECT ---'), t('B.tech'), t('MCA'), t('MBA'),t('Graduate')), 
    '#required' => TRUE, 
); 
$form['percentage'] = array(
    '#title' => t('Percentage/CGPA'), 
    '#type' => 'textfield', 
    '#required' => TRUE, 
); 
$form['application'] = array(
    '#title' => t('Job Applied'), 
    '#type' => 'select', 
    '#options' => array(t('---select---'),t('Web Developer'),t('Web Designer'),t('SEO'),t('Marketing')), 
    '#required' => TRUE, 
); 


$form['submit'] = array(
    '#value' => 'Submit', 
    '#type' => 'submit', 
); 
return $form; 

}

function form_test_form_submit($form, $form_state) { 
global $user; 
// Here u can insert Your custom form values into your custom table. 
db_insert('drupal') 
    ->fields(array(
     'name' => $form_state['values']['name'], 
     'college' => $form_state['values']['college'], 
     'education' => $form_state['values']['education'], 
     'percentage' => $form_state['values']['percentage'], 
     'application' => $form_state['values']['application'], 

    ))->execute(); 
drupal_set_message("successfully saved data"); 

} ?>

所有在一個文件(.module)....任何幫助將不勝感激。 謝謝。

回答

1

解決方法是將選項數組從一個函數傳遞到另一個函數。要使用「form_test_form」和「form_test_form_submit」函數的第二個參數。

function form_test_form($form,&$form_submit) { 
    $form_submit['myoptions'] = array(' select', 'option1' ..... 
} 

然後

function form_test_form_submit($form,&$form_state) {  
$myoptions = $form_state['myoptions']; etc ... 
} 
+0

感謝@progzy ....我與你it..can請指導我對somenice教程數據retrival從數據庫中的Drupal 7做了什麼?......我已經做到了這一點與PHP,但同樣不工作與Drupal – user1204 2014-09-08 05:41:55

+0

行。請確認答案,如果它適合你。 對於其他問題,請打開一個新的問題:) – progzy 2014-09-08 08:01:54