1

I have a webform created using the admin panel and would like to change one of its textfield values on click of a button. I have attempted doing this from hook_form_alter. I have given the wrapper name 'changeText' for this textfield from admin panel.

function fence_quote_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'webform_client_form_1'){
        $form['buttons']['fence_quote']  = array(
          '#type' => 'button',
          '#value' => t('Get online estimates'),
          '#ajax' => array(
              'callback' => 'fence_quote_form_callback', 
              'wrapper' => 'changeText',
          ),
        );
       $form['field_meters']['#prefix'] = '<div id="changeText">';
       $form['field_meters']['#sufix'] = '</div>';
    }   
}

function fence_quote_form_callback($form, &$form_state) {
   $form_state['values']['field_meters']['und'][0]['value'] = '111';
   return $form['wrapper'];
}

The problem is I can't update 'meters' value using AJAX callback as it is a field created from admin panel and not from hook_form_alter. However if I place

$form['submitted']['meters']['#default_value'] = '111'; 

outside of the callback it works on page refresh. How can I do this?


As requested by @kamal-oberoi I'm uploading a screenshot of my full code.

enter image description here

1

Finally I figured this out!

$form['submitted']['total']['#prefix'] = '<div id="changeText">';
$form['submitted']['total']['#suffix'] = '</div>';

$form['buttons']['fence_quote']  = array(
            '#type' => 'button',
            '#value' => t('Get online estimates'),
            '#ajax' => array(
              'callback' => 'fence_quote_form_callback', 
              'wrapper' => 'changeText', 
            ),
);

Then inside callback function

$form['submitted']['total']['#value'] = '111';
return $form['submitted']['total'];
1

add wrapper to your text field which you want to update.

 function fence_quote_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'webform_client_form_1'){
    $form['buttons']['fence_quote']  = array(
      '#type' => 'button',
      '#value' => t('Get online estimates'),
      '#ajax' => array(
          'callback' => 'fence_quote_form_callback', 
          'wrapper' => 'changeText',
      ),
    );
    /* Please add Wrapper to text_field */
    $form['wrapper']['your_textfield_name']['#prefix'] = '<div id="changeText">';
    $form['wrapper']['your_textfield_name']['#sufix'] = '</div>';
  }  

}


function fence_quote_form_callback($form, &$form_state) {
     $form_state['values']['meters']['und'][0]['value'] = '111';
     return $form['wrapper'];
 }
  • Thanks for your answer @kamal-oberoi but I'm unable to identify name of my field to add wrapper. The field key of it is 'meters' but when I type $form['meters']['#prefix'] = '<div id="newtext">' it doesn't recognise. – user2574948 Nov 9 '15 at 1:50
  • Also there's an error message saying 'Undefined index: wrapper in fence_quote_form_callback()' – user2574948 Nov 9 '15 at 2:04
  • I managed to identify the name of field. It's $form['submitted']['meters']['#prefix']. However I still get the error 'Undefined index: wrapper in fence_quote_form_callback()'. Seems like wrapper can't be returned that way. – user2574948 Nov 9 '15 at 2:35
  • Please change this $form['submitted']['meters']['#prefix'] to $form_state['values']['field_meters']['und'][0]['value'] – Kamal Oberoi Nov 9 '15 at 5:09
  • Still no luck.. throws this error message 'Undefined index: values in fence_quote_form_alter() ' – user2574948 Nov 9 '15 at 5:32

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.