Last active
September 24, 2015 12:06
-
-
Save netlooker/00f689e19151e150322c to your computer and use it in GitHub Desktop.
Drupal 7 - Form API - Populating field value by ajax request via ajax_command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Implements hook_form_FORM_ID_alter(). | |
*/ | |
function hook_form_id_form_alter(&$form, &$form_state) { | |
$form['field_text_1']['#prefix'] = "<div id='text-1-value'>"; | |
$form['field_text_1']['#sufix'] = "</div>"; | |
$form['submit'] = array( | |
'#type' => 'button', | |
'#value' => t('Search'), | |
'#ajax' => array( | |
'callback' => 'update_field', | |
'wrapper' => 'text-1-value', | |
'method' => 'replace', | |
'effect' => 'fade', | |
), | |
); | |
return $form; | |
} | |
function update_field($form, &$form_state) { | |
$form['field_text_1']['und'][0]['value']['#value'] = 'BAM'; | |
return array( | |
'#type' => 'ajax', | |
'#commands' => array( | |
ajax_command_replace("#text-1-value", render($form['field_text_1'])), | |
), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment