I am using Drupal 7. I want to rebuild a part of form when a button is clicked. My form looks like this:
$form['me'] = array(
'#type' => 'fieldset',
'#value' => t('name'),
'#prefix' => '<div id="me-wrapper>',
'#suffix' => '</div>'
);
It contains various text fields and a submit button. The submit button looks like
$form['me']['save'] = array(
'#type' => 'submit',
'#validate' => array('save_validate'),
'#submit' => array('save_submit'),
'#ajax' => array(
'wrapper' => 'me-wrapper',
'callback' => 'me_ajax_callback'
),
);
in the save_submit function I set $form_state['rebuild] = true;
in ajax callback function
function me_ajax_callback($form, $form_state) {
return $form['me'];
}
Now, what happens is first time when I click submit, the ajax returns data and replaces the wrapper. But, 2nd time when I do so, I get a POST Internal Server Error 500.
Looking around the Form API, I set $form_state['no_cache']=true;
in submit handler and that prevents the POST error but still the AJAX response is empty.
It works fine if, I reload the page. So, in short, the first time I hit submit I get an AJAX response. On the second time, the response is blank.
$form_state['values']
rather than the$form['me']
? because you won't get what you need with $form, you have to use$form_state['values']['me']
why ? : because when you submit the '$form_state` holds your post values.