I made a simple form in a module with a textfield and a submit button. Every time I type something into the textfield and submit, when the page loads, the default value is in the textfield. I followed the tutorial on http://www.badzilla.co.uk/Drupal-7-Tutorial--Form-Block-With-Template-File

Every #value property is set to whatever I type into the textfield under $form and $form_state except #default_value.

What am I missing?

Here is the code


function my_form_block_info() {
  $block = array();

  $block['my_form_block'] = array(
    'info' => t('My Form'),
    'weight' => 0
    return $block;

function my_form_block_view($delta) {
  $block = array();
  switch ($delta) {
    case 'my_form_block':
      $block['title'] = t('Sample form in block');
      $block['content'] = drupal_get_form('my_form_form');
  return $block;

function my_form_form($form, &$form_state) {
  $form['name'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => 'some default value',


  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('submit'),
    '#validate' => array('my_form_validate'),
    '#submit' => array('my_form_submit'),

  $form['#theme'][] = 'my_form';
  return $form;

function my_form_validate($form, &$form_state) {

return TRUE;

function my_form_submit($form, &$form_state) {

    drupal_set_message(t('Hello :name - how are you?', array(':name' => $form_state['values']['name'])));

    $input = $form_state['input']['name'];
    $form_state['values']['name'] = $input;

function my_form_theme() {

    return array(
        'my_form_form' => array(
            'arguments' => array('form' => NULL),
            'render element' => 'form',
            'template' => 'my_form',
  • You only need one of arguments or render element in a hook_theme() implementation (in this case you want render element). That might be throwing things off, but it might not. Try removing the arguments line and flushing the caches, and see if the problem persists
    – Clive
    Commented Mar 12, 2013 at 16:19
  • I removed the arguments line, but the problem persists. Also, I want to note that $form[#theme'][] should be my_form_form, and in the my_form_theme function, template should be 'my_form_form'. Commented Mar 12, 2013 at 16:41
  • It wasn't using the template file, but now it is.. Commented Mar 12, 2013 at 16:41
  • Do you have any other suggestions? I read the entire form API quickstart guide and it didn't help me. Even when I inspect the input element with Firebug, the value property is empty. Commented Mar 12, 2013 at 16:59
  • As side note, :name is not a correct t()-placeholder.
    – avpaderno
    Commented Mar 13, 2013 at 2:47

2 Answers 2


You need to save the form values as variables in your my_form_submit function using variable_set():

variable_set('myform_name_value', $form_state['input']['name']);

Then pull the stored value back out with variable_get() as the default value when displaying the form field in my_form_form():

$form['name'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => variable_get('myform_name_value', 'some default value'),

Note that when you set variables with a module in Drupal, you shouuld also include an .install file that deletes the variables if the module is ever uninstalled.

  • 1
    That's the model I was following in my answer. Note that this method will set this system wide, not per user or session unless you use variable names based upon the user. It will also call a cache clear of the variables each time the form is submitted.
    – Jimajamma
    Commented Mar 12, 2013 at 19:37

I would have thought you would be able to do something like this in your form handler...

'#default_value' => $form_state['post']['name'] ? $form_state['post']['name'] : 'some default value',

...but perhaps since this is in a block, it is being called "fresh" each time the page is displayed.

So, quick and dirty (and possibly all hideously wrong) the following does work...

'#default_value' => $_SESSION['foo_bar_baz'] ? $_SESSION['foo_bar_baz'] : 'some default value',

...if you put...

 $_SESSION['foo_bar_baz'] = $form_state['values']['name'];

...in your submit handler. At least for logged in users anyway, and will persist during his/her session.

Hopefully there is a "drupaler" way of doing this, but in the meantime, flame retardant suit on.

  • if you want this value to persist across users and sessions, then definitely the variable_get()/set() answer is the way to go. if that's the case, you can use system_settings_form() as well.
    – Jimajamma
    Commented Mar 12, 2013 at 19:46
  • This worked for every user. I need it to be unique for every user though. I've been looking into the session_cache module for this. Or do you know how to create unique session variables for anonymous users? Commented Mar 13, 2013 at 11:41
  • Assuming the various other drupal caches aren't getting in the way (you aren't caching this block or page I hope) you might be able to use one of the anon session modules to do this, I've never used them. I'd look into using cookies; they should work the same for both anon and auth users. The issue there of course is when you have a shared computer accessing your site. Joy, eh?
    – Jimajamma
    Commented Mar 13, 2013 at 11:50

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.