phpdrupal-form-submission

Drupal7 :submitt data in a multistep form from an interface to an another


I am working with drupal forms I created a multisteps form that allows the user to navigate between 3 pages or 3 interfaces. Once the user fill the required forms in the page2 the submitt function have to collect this information in an array an pass this array to the next page to be used in this page.I am looking for a proper way to submitt data of page2 to the next page from the submitt function

  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;

}

This is my function everything work fine but I want just to pass the data array to the next form, what would be the best solution to do that.


Solution

  • I found the solution myself :D to store data from an interface and use it in an another interface we can do this: in the submitt function we have to rebuilt the form to move to the new interface and then store the data in a new form

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

    In the next form where we want to use the submitted data we do the next.

    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;
    }