0

我创建了一个简单的用户表单,允许用户提供数据库名称,当用户提交时,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();
}
4

1 回答 1

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

在此函数中,您可以处理您的表单。

于 2013-11-11T11:29:49.900 回答