Use AJAX on a form with multiple buttons I have a form that has two text areas and three buttons, "Copy", "Reverse" and "Clear". I would like the buttons to, respectively, copy the text from one text area to the other, take the text from the first area and place the reverse of it in the other, and clear both text areas using AJAX.

I know how to do these functions in PHP but I can't seem to get the AJAX to work at all. Here is my existing code, any help  would be appreciated:

&lt;?php

    function produce_form() {
    $form['input'] = array(
        '#type' =&gt; 'textarea',
        '#title' =&gt; t('Input text'),
        '#prefix' =&gt; '&lt;div id="in-text"&gt;',
        '#value' =&gt; '',
        '#attributes' =&gt; array(
            'placeholder' =&gt; t('Enter some text here... '),
        ),
        '#suffix' =&gt; '&lt;/div&gt;',
    );

    $form['buttons'] = array(
        '#type' =&gt; 'fieldset',
        '#title_display' =&gt; 'invisible',
        '#attributes' =&gt; array(
            'style' =&gt; 'border: 0px; text-align: right',
            'class' =&gt; array('container-inline'),
        ),
    );

    $form['buttons']['copy'] = array(
        '#type' =&gt; 'button',
        '#value' =&gt; 'Copy text',
        '#ajax' =&gt; array(
            'callback' =&gt; 'copy_text',
            'wrapper' =&gt; 'out-text',
//          'method' =&gt; 'replace',
//          'effect' =&gt; 'fade',
        ),
    );

    $form['buttons']['reverse'] = array(
        '#type' =&gt; 'button',
        '#value' =&gt; t('Reverse text'),
    );

    $form['buttons']['clear'] = array(
        '#type' =&gt; 'button',
        '#value' =&gt; t('Clear text'),
    );

    $form['output'] = array(
        '#type' =&gt; 'textarea',
        '#title' =&gt; t('Output text:'),
        '#prefix' =&gt; '&lt;div id="out-text"&gt;',
        '#value' =&gt; '',
        '#attributes' =&gt; array(
            'placeholder' =&gt; t('Your text will appear here...'),
        ),
        '#suffix' =&gt; '&lt;/div&gt;',
    );

    return $form;
    }
    print render(produce_form());

    function copy_text($form, &amp;$form_state){
        $form['output']['#value'] = $form_state['values']['input'];
        return $return['output'];
    }
?&gt;
Just two problem with your form:


Remove #value element from 'input' element
replace return $return['output']; with return $form['output']; 


The problem with #value is that the element gets re-initialised on form render. 

Here is the working version(copy button) of your code:

&lt;?php


function playground_menu() {
  $items['playground/form'] = array(
    'title' =&gt; 'Form',
    'description' =&gt; '',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('playground_demo_form'),
    'access arguments' =&gt; array('access content'),
  );
  return $items;
}

function playground_demo_form($form, $form_state) {

  $form['input'] = array(
    '#type' =&gt; 'textarea',
    '#title' =&gt; t('Input text'),
    '#prefix' =&gt; '&lt;div id="in-text"&gt;',
    '#attributes' =&gt; array(
      'placeholder' =&gt; t('Enter some text here... '),
    ),
    '#suffix' =&gt; '&lt;/div&gt;',
  );

  $form['buttons'] = array(
    '#type' =&gt; 'fieldset',
    '#title_display' =&gt; 'invisible',
    '#attributes' =&gt; array(
      'style' =&gt; 'border: 0px; text-align: right',
      'class' =&gt; array('container-inline'),
    ),
  );

  $form['buttons']['copy'] = array(
    '#type' =&gt; 'button',
    '#value' =&gt; 'Copy text',
    '#ajax' =&gt; array(
      'callback' =&gt; 'copy_text',
      'wrapper' =&gt; 'out-text',
//          'method' =&gt; 'replace',
//          'effect' =&gt; 'fade',
    ),
  );

  $form['buttons']['reverse'] = array(
    '#type' =&gt; 'button',
    '#value' =&gt; t('Reverse text'),
  );

  $form['buttons']['clear'] = array(
    '#type' =&gt; 'button',
    '#value' =&gt; t('Clear text'),
  );

  $form['output'] = array(
    '#type' =&gt; 'textarea',
    '#title' =&gt; t('Output text:'),
    '#prefix' =&gt; '&lt;div id="out-text"&gt;',
    '#value' =&gt; '',
    '#attributes' =&gt; array(
      'placeholder' =&gt; t('Your text will appear here...'),
    ),
    '#suffix' =&gt; '&lt;/div&gt;',
  );

  return $form;
}

function copy_text($form, $form_state){
  $form['output']['#value'] = $form_state['values']['input'];
  return $form['output'];
}