2

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' => 'View the sample form',  
    'page callback' => 'mymodule_page',  
    'page arguments' => array('frontpage_form'),  
    'access callback' => TRUE,  
    'type' => 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' => t('Please, choose appropriate year'),  
    '#type' => 'radios',  
    '#options' => array(  
      t('2011'),  
      t('2012'),  
      t('2013')  
     ),  
  );  
  $form['submit'] = array(  
    '#type' => 'submit',  
    '#value' => t('Submit')  
  );  
  return $form;  
}

/**  
* Validate the form.  
*/  
function frontpage_form_validate($form, &$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, &$form_state) {  
  $name = $form_state['values']['years'];  
  drupal_set_message(t('Thanks for filling out the form, %name',  
  array('%name' => $name)));  
}
  • I understand your question to be: "How do I get submitted value from it?", the rest of the text in your question adds a great deal of ambiguity to that however. – Letharion Feb 17 '12 at 14:45
5

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 => 1,
  2 => 2,
  3 => 3
)

So if your form element looks like this:

$form['years'] = array(  
  '#title' => t('Please, choose appropriate year'),  
  '#type' => 'radios',  
  '#options' => 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, &$form_state) {
  frontpage_submitted_value($form_state['values']['some_value']);
}

function frontpage_submitted_value($new_value = NULL) {
  $value = &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;
}
  • That was a useful function, upvoted. – Letharion Feb 17 '12 at 14:55
  • Thanks, for your answer, but it's actually not what I wanted to resolve. (I admit that it's because of my vague question formulation.) – Oleksandr Bondarenko Feb 17 '12 at 15:10
  • @OleksandrBondarenko No problem, there are a few other points to note actually; I've updated the answer. Hope that helps a bit – Clive Feb 17 '12 at 15:20
  • Clive, all you wrote was very helpful to me, but one question remains unanswered for me. How do I get the submitted value in template.php? – Oleksandr Bondarenko Feb 17 '12 at 15:45
  • @OleksandrBondarenko I've update the answer again with a quick-fix, it should do the trick – Clive Feb 17 '12 at 15:58
3

Answering the actual question:

The user input from the form are available in $form_state['values'].

The way you formulated the question however doesn't make sense. You clearly already now the answer, since your code has

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

but you say you've tried print_r($form[values]); which obviously isn't going to work, since you're looking in the wrong variable. Then, why you would expect to find 'user_name' in the form I don't understand, since all you declare is 'years'.

2

In fact I was needed smth like this:

a way to get the submitted value into template.php:

In a module file

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

function frontpage_submitted_value($new_value = NULL) {
  if ($new_value) {
     cache_set('years',$new_value,'cache');
  }else{
     return cache_get('years');
  }
}

And then in a function in template.php

if ($submitted_value = frontpage_submitted_value()) {
  $vars['some_val'] = $submitted_value;
}
cache_clear_all('years','cache');

Clive's solution with &drupal_static(__FUNCTION__, FALSE); didn't work since, according to this, '... static variable technique only stores data for the duration of a single page load.'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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