0

我正在使用 drupal 表单,我创建了一个多步骤表单,允许用户在 3 个页面或 3 个界面之间导航。一旦用户在 page2 中填写了所需的表单,submitt 函数必须在一个数组中收集这些信息,然后将此数组传递给要在此页面中使用的下一页。我正在寻找一种将 page2 的数据提交到提交功能的下一页

  function my_module_form_submit($form, &$form_state) {
  $data = array(); //array of data that takes data from the current page forms
  switch ($form_state['stage']) { // stage have 3 values(3 id) corresponding to three interfaces of forms

    case 'page1':
      // some simples instructions
     break;
      case 'page2':
         //collect data from page2 forms
         $database_name =$form_state['values']['databasename'];
         $user_name =$form_state['values']['username'];
         $user_pass =$form_state['values']['userpass'];
         $host =$form_state['values']['host'];
         $port =$form_state['values']['port'];
         $database_driver =$form_state['values']['databasedriver'];

         $db_array = array(); // put the data in an array
         $db_array['database'] = $database_name;
         $db_array['username'] = $user_name;
         $db_array['password'] = $user_pass; 
         $db_array['host'] = $host; //localhost
         $db_array['port'] = $port; //localhost
         $db_array['driver'] = $database_driver; //mysql
         // some query after being connected to the database that return an array containing 
         // existing data tables names in the current database(works fine) data tables names are stored in the array $data

     $form_state['multistep_values'][$form_state['stage']] = $form_state['values'];
     $form_state['new_stage'] = my_module_move_to_next_stage($form,$form_state);
     // function that changes the id of current the page to go to the next page: forms
   break;
  case 'page3':
  // some instructions
  break;

  $form_state['multistep_values']['form_build_id'] = $form_state['values']['form_build_id'];
  $form_state['stage'] = $form_state['new_stage']; //change the id of the current page and rebuilt the form with the next page forms
  $form_state['rebuild'] = TRUE;

}

这是我的功能,一切正常,但我只想将数据数组传递给下一个表单,最好的解决方案是什么。

4

1 回答 1

0

我自己找到了解决方案:D 从一个界面存储数据并在另一个界面中使用它,我们可以这样做:在提交函数中,我们必须重建表单以移动到新界面,然后将数据存储在新表单中

  // submitt the first form
  $form_state['rebuild'] = TRUE; // rebuilt to go to the next form
  $form_state['storage']['myvalue'] =$data; // store the required data

在下一个我们想要使用提交数据的表单中,我们执行下一个。

function MY_MODULE_form($form, &$form_state) { // the next form

if (!empty($form_state['storage']['myvalue'])) { // call and use the submitted data
    $values=$form_state['storage']['myvalue'];
    //use the submitted data of the previous form in the new form
  }

 $form['new form'] = array(
    '#type' => 'radios',
    '#options' =>$values,
    );


  return $form;
}
于 2013-11-15T10:48:00.440 回答