3

How to get radio value from radio button by using the following code

$form['pd']= array(
  '#type' => 'radio',
  '#title' => t('Programme Day'),
  '#options' => array(0 => 'One Day Programme', 1 => 'Two or More Day Programmes'),
  '#required'=>TRUE
);

$form['one_day'] = array(
  '#title' => t('Date'),
  '#type' => 'date',
  '#description' => t('Please select the date of Programme'),
  '#required' => TRUE,
  '#date_format' => "d-m-Y h:i A",
  '#date_year_range' => -15:+1',
  '#states' => array('visible' => array(':input[name="pd"]' => array('value' => 0)))
);


$form['from_day'] = array(
  '#title' => t('From Date'),
  '#type' => 'date',
  '#description' => t('Please select the from date of Programme'),
  '#required' => TRUE,
  '#date_format' => "d-m-Y h:i A",
  '#date_year_range' => '-15:+1',
  '#states'=>array('visible'=>array(':input[name="pd"]'=>array('value'=>1)))
);

$form['two_day'] = array(
  '#title' => t('To Date'),
  '#type' => 'date',
  '#description' => t('Please select the To date of Programme'),
  '#required' => TRUE,
  '#date_format' => "d-m-Y h:i A",
  '#date_year_range' => '-15:+1',
  '#states'=>array('visible'=>array(':input[name="pd"]'=>array('value'=>1)))
);

I tried the following code but the value shows as array not value.

$prog_day = $form_state['values']['pd'];

$one_date = $form_state['values']['one_day'];
$from_date = $form_state['values']['from_day'];
$two_date = $form_state['values']['two_day'];
  • 2
    Are you sure the 'pd' #type must be 'radio'? Try 'radios' api.drupal.org/api/drupal/… – Shyghar Jun 5 '13 at 8:54
  • its not working its display the error msg "Array" – Bala Krishnan Jun 6 '13 at 8:58
  • try this $prog_day = $form_state['values']['pd'][0]; – Shyghar Jun 6 '13 at 10:53
  • Thanking you sir its working. but i cant get it $two_date = $form_state['values']['two_day']; date field value – Bala Krishnan Jun 6 '13 at 11:20
1

if you are looking for the submitted values you need to look in the $form_state and not the $form array.

use $form_state['values']['your field name'], depending on element type you might have ['und'][0] or just [0] on the end.

  • Any idea how to get the textual value related to the selected index within _form_submit ? – W.M. Dec 20 '16 at 8:46
0

Drupal collects the submitted values of form in the $form_state array by default so you will get the radio button value in following variable:

$form_state['values']['pd']
-1

you need a default_value to get the value !

$form['pd']= array(
  '#type' => 'radio',
  '#title' => t('Programme Day'),
  '#options' => array(0 => 'One Day Programme', 1 => 'Two or More Day Programmes'),
  '#required'=>TRUE,
  '#default_value' => 0,
);

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.