Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a radio button called 'phone_number_y_n'. The values are 0 = India, 1 = US, 2=UK, 3=Canada. I want to check the value (which button the user selected) and process based on that.

It seems a simple thing, but i am not able to find how can i do that check $form_state['values']['phone_number_y_n'] is not working. I don'e see any #values option when I do the dpm of $form_state.

Thanks for your help.

Here is the code:

<?php

function formalter_form_alter(&$form, &$form_state, $form_id){


  if($form_id == "form1_node_form"){

    $form['field_province'] = array (
       '#title'   => 'Provience',
       '#type'    => 'select',
       '#options' => _load_province(),
       '#ajax'    => array(
            'event'=>'change',
            'callback' =>'formalter_dropdown_city',
            'wrapper' => 'edit-field-city',
            'method' => 'replace',
            )
      );

  $options2 = array(
    0 => t('Zero'), 
    1 => t('One'), 
    2 => t('Two'), 
    3 => t('Three'),
       );

  $form['field_city'] = array(
       '#title'   => 'City',
       '#type'   => 'select',
       '#options' => $options2,
       '#prefix' => '<div id="edit-field-city">',
       '#suffix' => '</div>',

   );

  $options3 = array(
    0 => t('India'), 
    1 => t('US'), 
    2 => t('UK'), 
    3 => t('Canada'),
       );

    $form['field_phone_number_y_n'] = array (
       '#title'   => 'Phone number Y/N',
       '#type'    => 'radios',
       '#options' => $options3,
       '#prefix' => '<div id="edit-field-phone-number-y-n">',
       '#suffix' => '</div>',
       '#ajax'    => array(
            'event'=>'change',
            'callback' =>'formalter_phonenumber',
            'wrapper' => 'edit-field-phone-number',
            'method' => 'replace',
            )
      );

    $form['field_phone_number'] = array (
       '#title'   => 'Phone number',
       '#type'    => 'hide',
       '#value'   => 'none',
       '#prefix' => '<div id="edit-field-phone-number">',
       '#suffix' => '</div>',
      );

 }

}

function formalter_dropdown_city($form, $form_state) {

  $options2 = _load_city($form_state['values']['field_province']);
  $form['field_city'] = array(
       '#title'   => 'City',
       '#type'   => 'select',
       '#options' => $options2,
       '#prefix' => '<div id="edit-field-city">',
       '#suffix' => '</div>',

   );

  return $form['field_city'];
}

function formalter_phonenumber($form, $form_state) {

  $options3 = $form_state['values']['field_phone_number_y_n'];

  $form['field_phone_number'] = array(
       '#title'   => 'Phone number',
       '#type'    => 'textfield',
       '#value' => $options3,
       '#prefix' => '<div id="edit-field-phone-number">',
       '#suffix' => '</div>',

   );

  return $form['field_phone_number'];
}

function _load_province() {
  $province = array('- Select province -');

  $query = db_select("sandbox_province", "a");
  $query->fields("a", array('id', 'name'));
  $query->condition("a.status", 1);
  $query->orderBy("a.name");
  $result = $query->execute();

  while($row = $result->fetchObject()){
    $province[$row->id] = $row->name;
  }

  return $province;
}


function _load_city($province_id) {
  $city = array('- Select city -');

  $query = db_select("sandbox_city", "a");
  $query->fields("a", array('id', 'name'));
  $query->condition("a.status", 1);
  $query->condition("a.province_id", $province_id);
  $query->orderBy("a.name");
  $result = $query->execute();

  while($row = $result->fetchObject()){
    $city[$row->id] = $row->name;
  }

  return $city;
}

Also, i want to display the label associated with the selected radio button instead of the value 0,1,2 or 3.

share|improve this question
    
$form_state['values']['phone_number_y_n'] will either have the value 0 or 1 depending on what you have selected. Are you sure that this is not the case? –  Mohammed Shameem Jan 18 '13 at 6:30
    
This code is in my custom form_alter module. When I do dpm($form_state['values']['phone_number_y_n']), i get undefined index. But I am selecting one of the radio buttons, the default is set to "No". –  MTh Jan 18 '13 at 19:25
1  
could you paste the code for clarity –  Mohammed Shameem Jan 18 '13 at 19:26
    
I re-posted question with the code, please see this drupal.stackexchange.com/questions/57365/… –  Mth Jan 19 '13 at 5:04
1  
In any AJAX callback you should take the reference of $form_state variable, like this: formalter_phonenumber($form, &$form_state) –  subhojit777 Jan 19 '13 at 5:24

2 Answers 2

put the field_phone_number in an if condition as given below with (This is inside the formalter_form_alter function where you have placed the hidden field. that should be removed and place the below code.

if (!empty($form_state['values']['field_phone_number_y_n'])) 
{
    $options3 = $form_state['values']['field_phone_number_y_n'];

  $form['field_phone_number'] = array(
       '#title'   => 'Phone number',
       '#type'    => 'textfield',
       '#value' => $options3,
       '#prefix' => '<div id="edit-field-phone-number">',
       '#suffix' => '</div>',

   );
}

Now the call back should simply return the field_phone_number so your callback funtion would be.

function formalter_phonenumber($form, $form_state) {

  return $form['field_phone_number'];
}
share|improve this answer
    
Thank you for your answer –  Mth Jan 19 '13 at 17:20

If you need the value within the form you should use ajax for that.

See ajax_example in the examples module for simple implementations similar to yours.

During submit, in form_submit the $form_state will have $form_state['values'].

If you want the value outside, you can use variable_get()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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