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'];
4
  • 2
    Are you sure the 'pd' #type must be 'radio'? Try 'radios' api.drupal.org/api/drupal/…
    – Shyghar
    Jun 5, 2013 at 8:54
  • its not working its display the error msg "Array" Jun 6, 2013 at 8:58
  • try this $prog_day = $form_state['values']['pd'][0];
    – Shyghar
    Jun 6, 2013 at 10:53
  • Thanking you sir its working. but i cant get it $two_date = $form_state['values']['two_day']; date field value Jun 6, 2013 at 11:20

3 Answers 3

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.

1
  • Any idea how to get the textual value related to the selected index within _form_submit ?
    – W.M.
    Dec 20, 2016 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 and acknowledge you have read our privacy policy.

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