Getting the value submitted from form with FAPI radios in template.php I have a form with radios on it. I do submit on the same page (there's no $form['#action'] assigned in the below code). What I want to do is to get submitted year in template.php so that I can define my logic based on received value.

To check that's all work fine I wanted print_r the contents of form['values'].
I did that in submit function but it didn't work. Actually, I can't print anything from both validate and submit functions.

So my question is how I can get the submitted value to template.php.

I work with the following code in my module:

function my_module_menu() {  
  $items['formexample'] = array(  
    'title' =&gt; 'View the sample form',  
    'page callback' =&gt; 'mymodule_page',  
    'page arguments' =&gt; array('frontpage_form'),  
    'access callback' =&gt; TRUE,  
    'type' =&gt; MENU_NORMAL_ITEM  
  );  
  return $items;  
}  

function mymodule_page() {    
  $output = print_r($_POST, true);  
  $output .= drupal_get_form('mymodule_form');  
  return $output;  
}  

/**  
* Define a form.  
*/  
function frontpage_form() {  
  $form['years'] = array(  
    '#title' =&gt; t('Please, choose appropriate year'),  
    '#type' =&gt; 'radios',  
    '#options' =&gt; array(  
      t('2011'),  
      t('2012'),  
      t('2013')  
     ),  
  );  
  $form['submit'] = array(  
    '#type' =&gt; 'submit',  
    '#value' =&gt; t('Submit')  
  );  
  return $form;  
}

/**  
* Validate the form.  
*/  
function frontpage_form_validate($form, &amp;$form_state) {  
  if ($form_state['values']['user_name'] == 'King Kong') {  
    // We notify the form API that this field has failed validation.  
    form_set_error('user_name',  
    t('King Kong is not allowed to use this form.'));  
  }  
}

/**  
* Handle post-validation form submission.  
*/  
function frontpage_form_submit($form, &amp;$form_state) {  
  $name = $form_state['values']['years'];  
  drupal_set_message(t('Thanks for filling out the form, %name',  
  array('%name' =&gt; $name)));  
}
I think the problem is that your #options array for the radios isn't keyed (which it should be). PHP will automatically assign the keys as 0,1,2 etc but 0 is not valid as Drupal ignores such a value which may be causing you a problem, and I guess you're expecting a value of 2011, 2012 or 2013 which currently you won't get.

Drupal does provide a handy function, drupal_map_assoc to create a keyed array from a flat one. This code:

$array = drupal_map_assoc(array(1, 2, 3));


will produce an array of

array (
  1 =&gt; 1,
  2 =&gt; 2,
  3 =&gt; 3
)


So if your form element looks like this:

$form['years'] = array(  
  '#title' =&gt; t('Please, choose appropriate year'),  
  '#type' =&gt; 'radios',  
  '#options' =&gt; drupal_map_assoc(array(2011, 2012, 2013))
);


In your submit function you would grab the selected value with:

$form_state['values']['years'];


I think you might have a slight confusion in your logic, this bit of code:

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


is fine to grab values from the $form_state array but years is not a name, it's a year. That might also be adding to the confusion.

EDIT

I've just noticed a few other problems in your code that may be causing unexpected results.

The first one is in mymodule_page(). drupal_get_form() returns an array in Drupal 7 so concatenating it to a string (the return from print_r()) will not produce the results you're expecting. 

Secondly, you're calling drupal_get_form('mymodule_form'); and your form is actually named frontpage_form().

Lastly, you don't actually need the mymodule_page() function at all...you can change the page callback of your menu item to drupal_get_form() and the form will automatically be built for you on that page. Remember to clear Drupal's cache once you've made this change.

The last thing I'd say is that relying on print_r for debugging can be very tedious. Instead, I'd advise installing the Devel Module which has a wonderful function called dpm(). This will print a nicely formatted representation of any variable to the standard messages area when you call it. It's a must for Drupal development really.

You'd use it like this, for example in your form submit handler:

dpm($form_state['values']);


EDIT 2

Quick dirty way to get the submitted value into template.php:

In your module file

function frontpage_form_submit($form, &amp;$form_state) {
  frontpage_submitted_value($form_state['values']['some_value']);
}

function frontpage_submitted_value($new_value = NULL) {
  $value = &amp;drupal_static(__FUNCTION__, FALSE);

  if ($new_value) {
    $value = $new_value;
  }

  return $value;
}


And then in a function in template.php

if ($submitted_value = frontpage_submitted_value()) {
  $vars['some_val'] = $submitted_value;
}