1

I created a custom form and after the form is submitted I need to display the submitted data in a table. This seems like a very simple problem. I am able to use dpm() and drupal_set_message() to display data submitted from the form in mymodule_form_submit($form, &$form_state); however, I can't output it to a table.

I have 3 field values

$form_state['redirect'] = 'some/path/' . $form_state['values']['my_value'];

Using this I can send just 1 value. But how can I send an array with this? Query string may appear to be very long.

1
  • Welcome to Drupal answers. You forgot mention a critical part, your Drupal version.
    – No Sssweat
    Commented May 23, 2016 at 7:37

2 Answers 2

0

For Drupal 7:

function youth_fest_form($form, &$form_state) {

    $form['can_id'] = array(
             '#type'=>'hidden',
             '#value' => isset($value['can_id']) ? $value['can_id'] : '' ,
         );


    $form['firstname'] = array(
            '#type' => 'textfield',
            '#title' => 'Full Name',
            '#size' => 30,
            '#maxlength' => 50,
            '#weight'=>1,
            //'#default_value'=>t('First Name'),
            '#required' => TRUE,
            '#attributes' => array(
                'placeholder' => t('First Name'),
                ),
            '#prefix' => '<div id="wrapper"><div id="leftcolumn">',
            '#suffix' => '</div>',

        );



    $form['submit_button'] = array(
        '#type' => 'submit',
        '#value' => t('SUBMIT'),
        '#weight'=>13,

      );


      return $form;

    }
function youth_fest_form_submit($form, &$form_state) {
$data = db_insert('youth')
  ->fields(array(
    'first_name'        =>  $form_state['values']['firstname'],
    ))
  ->execute();
if(isset($data)){
    drupal_set_message('Values inserted successfully!');
  }
}

submitted result in a table:

function youth_admin_content_page(){

 $header = array('First Name', 'Last Name');

  # set the database table
  $query = db_select('youth', 'p')->extend('PagerDefault');

  # get the desired fields from the database
   $query->leftJoin('file_managed', 'fm', 'fm.fid = p.file');

   $query->fields('fm',array('filename','uri'));

   $query->fields('p', array('candidate_id', 'first_name'))
    ->limit(3);

  # execute the query
  $results = $query->execute();

  # build the table fields
  $rows = array();
  foreach ($results as $row) {




    $rows[] = array("<a href=\"/projects/edit/" . $row->candidate_id . "\">" . $row->first_name . '</a>',
                    $row->last_name,
                   '<img src="'.$optimageurl.'" width="100" height="100">',

    );
  }
  $output = theme('table', array('header' => $header,'rows' => $rows ));
  $export = 'Export';
  # add the pager
  $output .= theme('pager');
  return $output;

}
5
  • @Sugandha - Hi, Iam not saving the data in the db. I just want to create a normal html to display the submitted values. i.e after submit it will redirect to a different page, where i want to print the $form_state['values'] values
    – akshata
    Commented May 23, 2016 at 8:39
  • @hint: drupal_set_message(theme('table', array('header' => $excel_header, 'rows' => $excel_rule_exceptions)),'error'); Commented May 23, 2016 at 8:50
  • Can you explain? I want the results to be displayed in a different page. Not just show it in the message box.
    – akshata
    Commented May 23, 2016 at 8:52
  • create a menu with page callback and above piece of code into that callback and if you don't want any message box then simply theme drupal_set_message using preprocess in template. Commented May 23, 2016 at 8:57
  • using preprocess in template Can you explain?
    – akshata
    Commented May 24, 2016 at 5:20
0

You don't need to redirect the form like that: a form submission handler can pass data to the form builder using $form_state; in this way, you don't have any limit for what you can pass back to the form builder.

It's enough the form submission handler uses the following line.

$form_state['rebuild'] = TRUE;

Then it saves in $form_state the values to pass back to the form build, being careful not to use the same index used for other purposes, from Drupal, or your module. Usually, $form_state['storage'] is used for that purpose, as explained in $form_state keys.

$form_state['storage']
When building complex forms that require multiple steps for completion (for example, a three-page survey), it's necessary to preserve the data from all steps so that they can be processed together at the end. (And, on occasion, to vary the contents of one step based on the input from the previous one). Any data placed in the 'storage' bin of the $form_state collection will automatically be cached and re-loaded when the form is next submitted, allowing your code to accumulate data from step to step and process it in the final stage without any additional code. Developers who want more control can use their own caching mechanisms to store temporary form data (the user session and hidden form fields are two popular alternatives), but the 'storage' bin is automatically handled for you by the form API.

That is for Drupal 6; for Drupal 7, see drupal_build_form().

storage: $form_state['storage'] is not a special key, and no specific support is provided for it in the Form API. By tradition it was the location where application-specific data was stored for communication between the submit, validation, and form builder functions, especially in a multi-step-style form. Form implementations may use any key(s) within $form_state (other than the keys listed here and other reserved ones used by Form API internals) for this kind of storage. The recommended way to ensure that the chosen key doesn't conflict with ones used by the Form API or other modules is to use the module name as the key name or a prefix for the key name. For example, the Node module uses $form_state['node'] in node editing forms to store information about the node being edited, and this information stays available across successive clicks of the "Preview" button as well as when the "Save" button is finally clicked.

If you really need to print the data in another page, then save the data in $_SESSION, instead of passing that as GET data.

2
  • Hi, I just want to create a normal html to display the submitted values. i.e after submit it will redirect to a different page, where i want to print the $form_state['values'] values. Maybe the example that I gave was wrong, but can you tell how I can pass the $form_state['values'] or any array after form submit to a custom tpl file.
    – akshata
    Commented May 23, 2016 at 8:42
  • Just save the data in the session.
    – avpaderno
    Commented May 23, 2016 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.