如何以drupal形式处理提交的值

时间:2013-11-11 11:19:18

标签: php drupal drupal-7

我创建了一个简单的用户表单,允许用户提供数据库名称,当用户提交drupal站点时将连接到给定的数据库名称,我在文件settings.php:myDataBase中定义了我的数据库,当我尝试

  db_set_active('myDataBase');

它工作正常,但是当我在文本字段中输入myDataBase并提交然后使用提交的值连接到数据库时,它没有用。

<?php 
 function testform_menu() {
 $items = array();
 $items['test'] = array(
 'title' => 'connect an external database',
 'page callback' => 'drupal_get_form',
 'page arguments' => array('testform'),
 'access callback' => TRUE,
 'type' => MENU_NORMAL_ITEM,
 );

  return $items;
 }

 function testform($form, &$form_state) {
  $form = array();
  $form['Databasename'] = array(
   '#type' => 'textfield',
   '#title' => t('enter the database name'),
   '#description' => t('a user interface to switch between databases'),
   '#size' => 28,
   '#required' => TRUE,

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


  return $form;
}

function testform_validate($form, &$form_state) {

}

function testform_submit($form, &$form_state) {

if (db_set_active('myDataBase'))
{
    drupal_set_message('database connected'); // works
}

 //   $database_name =$form_state['values']['Databasename']; // ius not working!!
 //    if ($database_name ) {
 //    db_set_active($database_name); // I even tried to parsing the submitted value to a string but didnt work in both case
 //    drupal_set_message(t('connected to the database'.' '.$database_name));
 //    }
db_set_active();
}

1 个答案:

答案 0 :(得分:0)

您需要一个功能来管理表单提交,例如:

function testform_submit($form, &$form_state)
{
    //Do your stuff
}

或者您可以使用表单中的#submit键定义提交功能:

'#submit' => array('your_custom_function_name')

更多信息:https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#submit_property

在此功能中,您可以处理表单。