<?php

function test_module_menu() {

$items = array();

$items['test'] = array(
    'title' => 'Test form resubmit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('test_module_test'),
    'access arguments' => FALSE,
    'type' => MENU_CALLBACK,
  );
$items['test_module/js'] = array(
    'title' => 'Ahah submit',
    'page callback' => 'test_module_js',
    'access callback' => '_test_module_access_js',
    'access arguments' => FALSE,
    'type' => MENU_CALLBACK,
  );
  
  $items['test_module_change/js'] = array(
    'title' => 'Ahah submit',
    'page callback' => 'test_module_change_js',
    'access callback' => '_test_module_access_js',
    'access arguments' => FALSE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function _test_module_access_js() {
    return TRUE;
}

function test_module_test(&$form_state) {

    $form['sb1'] = array(
    '#type' => 'submit',
    '#value' => t('SB1'),
    '#submit' => array('test_module_sb1_submit'),    
  );
  
  $form['val'] = array('#value' => '<div id="test_module_replace"> This is it </div>');
  
   $form['sb2'] = array(
    '#type' => 'submit',
    '#value' => t('SB2'),
    '#submit' => array('test_module_sb2_submit'),    
  );
  
  $form['sb2']['#ahah'] = array(
      'path' => 'test_module/js',
      'wrapper' => 'test_module_replace',
      'effect' => 'fade',
    );
    
    if($form_state['resub']) {
    
    $form['sb2'] = array(
    '#type' => 'submit',
    '#value' => t('SB2'),
    '#submit' => array('test_module_sb2_change_submit'),    
  );
  
  $form['sb2']['#ahah'] = array(
      'path' => 'test_module_change/js',
      'wrapper' => 'test_module_replace',
      'effect' => 'fade',
    );
    
    }
    
    return $form;
    
    
    
}

function test_module_sb1_submit(&$form, &$form_state) {
$form_state['rebuild'] = TRUE;
$form_state['resub'] = TRUE;
}

function test_module_sb2_submit(&$form, &$form_state) {
drupal_set_message(t("In submit 1"));
}

function test_module_sb2_change_submit(&$form, &$form_state) {
drupal_set_message(t("changed to submit 2"));
}


function test_module_js() {

 $form_state = array('storage' => NULL, 'submitted' => FALSE);
 $form_build_id = $_POST['form_build_id'];
 $form = form_get_cache($form_build_id, $form_state);
 $form_state['post'] = $form['#post'] = $_POST;
 $form['#programmed'] = $form['#redirect'] = FALSE;
 $args = $form['#parameters'];
 $form_id = array_shift($args);
 drupal_process_form($form_id, $form, $form_state);
 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
 //Get HTML for the replacement form. Only these elements will be AHAH-refreshed.
 $new_form = array();
 
 $new_form['element'] = array('#value' => "Addition by submit 1");
    $output = theme('status_messages') . drupal_render($new_form);
    //Return the results.
    drupal_json(array('status' => TRUE, 'data' => $output));

}
function test_module_change_js() {

 $form_state = array('storage' => NULL, 'submitted' => FALSE);
 $form_build_id = $_POST['form_build_id'];
 $form = form_get_cache($form_build_id, $form_state);
 $form_state['post'] = $form['#post'] = $_POST;
 $form['#programmed'] = $form['#redirect'] = FALSE;
 $args = $form['#parameters'];
 $form_id = array_shift($args);
 drupal_process_form($form_id, $form, $form_state);
 $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
 //Get HTML for the replacement form. Only these elements will be AHAH-refreshed.
 $new_form = array();
 
 $new_form['element'] = array('#value' => "Never gets added");
    $output = theme('status_messages') . drupal_render($new_form);
    //Return the results.
    drupal_json(array('status' => TRUE, 'data' => $output));

}
