Hello,

I would like to see the structure of $form_state but this code is not working. It seems my submit hook is completely ignored, while my form_alter hook works fine:

function mymodulename_form_submit($form, &$form_state) {
print_r($form_state['values']);
}

Comments

clau_bolson’s picture

Hi Giorgio,
Yesterday I spent a whole saturday afternoon trying to understand how to create a page that would take input from a form and just display it.

<?php
echo $_POST['name];
?>

A trivial piece of PHP code, yet not trivial in Drupal.

The answer is that if you don't process your data in hook_form_submit OR you include a redirect, the form will just be displayed again and your data will be lost.

This is how I solved it - I don't know if this is the best solution, but it worked for me...

I include the whole module named "ws". I use a block to display the form and a page to show the result.

<?php
// $Id$

/**
 * @file
 * Simple form test
 */

/**
 * Implementation of hook_block
 */
function ws_block($op='list' , $delta=0, $edit=array()) {
  switch ($op) {
    case 'view':
      $blocks['content'] = drupal_get_form('ws_form');
      return $blocks;
  }
}

/** 
 *Show form
 */

function ws_form($form_state) {
    $form['name'] = array (
        '#type' => 'textfield',
        '#title' => 'Name',
    );

    $form['submit'] = array (
        '#type' => 'submit',
        '#value' => 'Send',
    );
return $form;
}

/**
 * Here the form is processed, the variable "name" is sent as a parameter to the ws function

function ws_form_submit($form, &$form_state) {
    $form_state['redirect'] = array('ws','name='.$form_state['values']['name']); //IMPORTANT!!
    return;
}
/**
 * Implementation of hook_menu
*/

function ws_menu() {
    $items['ws'] = array (
        'page callback' => 'show_name',
        'access callback' => TRUE,
        'type' =>  MENU_CALLBACK,
        );
return $items;
}

/**
 * This function gets the parameter and shows it
*/

function show_name($name) {
return 'Hello '.$name;
}

That's it! After you enter your name, say 'John', and submit the form, you are redirected to q=ws/John, which reads Hello John.

jaxxed’s picture

It doesn't work to put output in the form submit, unless the submit fails.

THIS IS VERY LATE, BUT MAY BE USEFULL TO OTHERS WHO HAVE THE SAME QUESTION

I haven't checked Clau's response much, but I would like to offer some advice, on how I do this.

First: most likely your form submit is happening, and content is being written to the screen, however as Clau suggested, the page is then being redirected back to the form. This is the expected behaviour with form submits using the form system.

Ok, so now I can think of a couple of debugging situations in which you'd want to print_r the form_state, so I'll respond as though this is the case.
The simplest solution is to simple add a die after the print_r. This means that your app will stop after your debugging code has been executed.

function my_form_submit($form,$form_state) {
  print_r($form_state);
  die("\n<br/>DIE");
}

Here you should get a white screen with your print_r output on the page.

If you need to keep the redirect, or are planning on outputting something more asethetic to be used in the actualy application, then consider using the drupal_set_message function. Drupal_set_message will place the output into session space, where it will be pulled on the next valid page load, and placed in the messages section of the page template. This only works if you haven't disabled Drupal messages, either by configuration, or by removing the template variable from your theme.

function my_form_submit($form.$form_state) {
  drupal_set_message( 'Here are the form contents:' . var_export($form_state , true) );
}

You'll likely want something prettier than what I've put.

giorgio79’s picture

Or even better, install the Devel module, and use dpm() function call

jaypan’s picture

I have my own module of various debugging functions I use. Two of the most common functions I use are:

function p($item)
{
  drupal_set_message('<pre>' . print_r($item, true) . '</pre>');
}

function d($item)
{
  die('<pre>' . print_r($item, true) . '</pre>');
}

p() dumps the data in a message, much the same as devel's dsm() function (I think - I've never actually used dsm()), and d() kills the process right then and there and dumps the contents to the screen.

Contact me to contract me for D7 -> D10/11 migrations.

johnlaine’s picture

This works beautifully and so simple. Thanks!

hondaman900’s picture

So dsm($form_state); works fine for me in myform_submit, and I get a complete dump of form data, including an array of input values and individual myfield values.

However, the following do not return anything:

dsm($form_state->input);

dsm($form_state->input->myfield->value());

Why not? What am I missing....?

jaypan’s picture

Assuming that you're using Drupal 8 (your code would indicate that), input is a protected property of the form state, and therefore cannot be accessed externally as you are trying to do. There is a getter function for getting the input, $form_state->getUserInput(). However, you should almost never be accessing input value directly as they are unsanitized, and doing so can open up security holes in your code. You should be using $form_state->getValues().

Contact me to contract me for D7 -> D10/11 migrations.

hondaman900’s picture

Thank you for the quick response. My project is Drupal 7 and yes, I will validate my data, but right now I'm just trying to get the basic functionality to work.

I have a form, pre-populated by existing data from entityform submission data using an entity_metadata_wrapper in my hook_form function. I'm trying to determine how I get changes made by the user back to the database using the wrapper. The wrapper I define and use in the hook_form function doesn't seem to have scope outside that function so I need to determine how to get a handle on my form input so that I can re-wrap it and save back to the database in the form_submit function. I was hoping that I could use dsm() to interrogate what the Form API is giving me in results and then wrap that and save back to the database.

Maybe I'm missing something or there's an easier way...

jaypan’s picture

As I said above, you can use $form_state->getValues().

Contact me to contract me for D7 -> D10/11 migrations.

hondaman900’s picture

Do you mean use

dsm($form_state->getValues());...?

as that gives me a fatal error

Fatal error: Call to a member function getValues()

hondaman900’s picture

After much searching I found the correct syntax.

dsm($form_state['input']);

and

$my_variable = $form_state['input']['my_field_name'];
dsm($my_variable);

work great. Now I can handle my form input and save to database.

However, I'm not quite sure why I have to pass to a variable and can't just use:

dsm($form_state['input']['my_field_name']);

Thank you for all the help - much appreciated.