Via Tweets

AJAX with multi field

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

$counter=1;
    if(!empty($form_state['values']['counter'])){
        //Increment the counter
        $counter = $form_state['values']['counter']+1;
       
        for($i = 1; $i <= $counter; $i++ ){       
            $form['description' . $i] = array(
                '#title' => t('description-' . $i),
                '#type' => 'text_format',
                '#format' => 'full_html',
                '#description' => t('Insert the text for description-' . $i),
                '#default_value' => '',
                '#value' => '',
            );        
        }       
       
    }
   
    $form['counter'] = array(
        '#type' => 'hidden',
        '#value' => $counter,
    );    

$form['actions']['save_relation'] = array(
        '#type' => 'button',
        '#value' => t('save'),
        '#ajax' => array(
            'callback' => 'ajax_multifield_callback',
            'wrapper'  => 'replace-description-div',
            'method' => 'append',
            'effect' => 'fade',
        ),
    );

 

}

 

function ajax_multifield_callback($form, $form_state) {
    //Take the counter value
    $counter = $form_state['values']['counter']; 
  
    return  $form['description' . $counter];
           
   
}