2

I have a select field in my custom module (Drupal 7), which needs to refresh the values of 2 different field types when a value is selected, firstly a checkbox field, which currently is refreshed correctly when am changing the dropdown value, and a text field, which is not changing, as it keeps the value from the first dropdown option by default.

Samples of my codes:

$form['webform_name'] = array(
'#type' => 'select',
...
#ajax' => array(
  'event' => 'change',
  'callback' => 'ajax_checkbox_callback',
  'method' => 'replace',
  'wrapper' => array('checkboxField','txtField'),
)

$form['checkboxField'] = array(    
'#type' => 'checkboxes',
...
'#prefix' => '<div id="checkboxField">',
'#suffix' => '</div>',

$form['txtField']=array(
'#type' => 'textfield',
...
'#prefix' => '<div id="txtField">',
'#suffix' => '</div>',


function ajax_checkbox_callback($form, &$form_state) {
    return array(
    '#type' => 'ajax',
    '#commands' => array(
    ajax_command_replace("#checkboxField", render($form['checkboxField'])),
    ajax_command_replace("#txtField", render($form['txtField']))
    )
  ); 
}

How can I make the txtField field to clear and load the corresponding value every time I change my drop-down values?

2 Answers 2

3

In your ajax_command_replace where the form is rendered, do this: ajax_command_replace("#checkboxField", render($form['checkboxField']['values']=$text)), where $text is the value from the checkboxes, as shown below:


function ajax_checkbox_callback($form, &$form_state) {
    return array(
    '#type' => 'ajax',
    '#commands' => array(
    ajax_command_replace("#checkboxField", render($form['checkboxField']['values'] = $value_text_form_text_field_1)),
    ajax_command_replace("#txtField", render($form['txtField']['values'] = $value_text_form_text_field_2))
    )
  ); 
}

0

Well, I believe wrapper should be a string and not an array. So can't you just change it to string :

wrapper => 'checkbox_textfield'

Something like above and change code to :

$form['checkbox_textfield'] = array(
  '#prefix' => '<div id="checkbox_textfield">',
  '#suffix' => '</div>',
);
$form['checkbox_textfield']['checkboxField'] = array(
  ...
);

$form['checkbox_textfield']['txtField']=array(
  ...
);
2
  • I tried the method proposed, but then my list of checkboxes stopped refreshing when changing my drop-down values. Any other approach for this?
    – st2014
    Commented Jun 25, 2015 at 10:52
  • I don't know if 'method' => 'replace', is correct? Try to remove event and method too from the #ajax array. Commented Jun 25, 2015 at 11:00

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.