Form built by Ajax commands not sending values My problem has to do with Ajax commands and the form being rebuilt after calling ajax_command_append().

I have a form with two elements an option and a textfield. These are built-in with the form on page load. Then, in an Ajax callback function I add two other fields, i.e. another textfield and a text area.

The form runs and rebuilds perfectly fine, but when I try to read the values typed in or stored-from-beginning, nothing happens except that only the values for the first two fields that were originally built-in with the form are displayed.

I think the problem is either 1) I am not rebuilding the (same?) form properly, or 2) I am not calling the elements/fields properly when reading values.

Any hints will be greatly appreciated

Here is a simplified version of the test code I gathered from various examples:

function module_form($form, &amp;$form_state) {
$form = array();
 $form['changethis'] = array(
 '#type' =&gt; 'select',
 '#options' =&gt; array(
  'one' =&gt; 'one',
  'two' =&gt; 'two',
  'three' =&gt; 'three',
  ),
   '#ajax' =&gt; array(
     'callback' =&gt; 'ajax_example_simplest_callback',
     'wrapper' =&gt; 'replace_textfield_div',
     'method' =&gt; 'replace',
     'effect' =&gt; 'fade',      
    ),
  );
  $form['replace_textfield'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t("The default value will be changed"),
    '#description' =&gt; t("Say something about why you chose") . "'" .     (!empty($form_state['values']['changethis']) ? $form_state['values']['changethis'] :     t("Not changed yet")) . "'",
    '#prefix' =&gt; '&lt;div id="replace_textfield_div"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
   );
 $form['box'] = array(
   '#type' =&gt; 'markup',
   '#prefix' =&gt; '&lt;div id="box"&gt;',
   '#suffix' =&gt; '&lt;/div&gt;',
   '#markup' =&gt; '&lt;h1&gt;Initial markup for box&lt;/h1&gt;',
  );

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#ajax' =&gt; array(
      'callback' =&gt; 'ajax_example_submit_driven_callback',
      'wrapper' =&gt; 'box',
     ),
    '#executes_submit_callback' =&gt; TRUE,
    '#value' =&gt; t('Submit'),

   );
  return $form;
}



function ajax_example_simplest_callback($form, $form_state) {

   $commands = array(); 

   $commands[] = ajax_command_append(NULL, '&lt;div id=replace_textfield_div&gt;&lt;input name=replace_textfielddd id=replace_textfielddd value=79898&gt;test data&lt;/input&gt;&lt;/div&gt;&lt;br&gt;');

  $commands[] = ajax_command_append(NULL, '&lt;br&gt;&lt;textarea rows=4 cols=30 name=replace_textarea id=replace_textarea&gt;Changethis value: '. $form_state['values']['changethis'] .'&lt;/textarea&gt;');
  $page = array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);

  ajax_deliver($page);

  //print ajax_render($commands);       // not helping 
  //drupal_exit();

 // return $form['replace_textfield'];  // not helping 
 }




function ajax_example_submit_driven_callback($form, $form_state) {
   $form_state['rebuild'] = true;  // not helping 
   $element = $form['box'];
   $element['#markup'] = "Clicked submit ({$form_state['values']['op']}): " .  $form_state['values']['replace_textfield']. " - ".  $form_state['values']['replace_textfielddd'] ." -.";
   return $element;
}


Thanks

EDIT

I also tried calling this function from the inside of command:
 function display_my_form(){

 $form = drupal_get_form('CPR_Calculator_form');

 $form['replace_textfielddd'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t("bye"),
    '#description' =&gt; t("ummm"),
    '#prefix' =&gt; '&lt;div id="replace_textfield_divvv"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );
  $display = '&lt;div id="replace_textfield_divvv"&gt;';
  $display .= render($form);
  $display .= '&lt;/div&gt;';

  return $display;
}


and then in the callback I replaced the ajax_command_append() with this line:

$commands[] = ajax_command_replace(NULL, display_my_form());
You need to build conditional elements in the form definition (mymodule_form) and not in the ajax callback. When the form is rebuilt using ajax, $form_state['values'] will be set, whereas it's not set on the initial form build.

Here is an example:

function mymodule_form($form, &amp;$form_state)
{
  if(isset($form_state['values'])
  {
    // put your conditional form elements here
  }
}