Keep form values after form submit

Sometimes form state values need to be kept to keep filter values on a form submit. In order to do this, the $form_state['rebuild'] boolean has to be set to true. This is done in the submit handler of the form.


/**
 * Implements hook_form().
 */
function MYFORM_form($node, &$form_state) {

  $form = array();

  $form['filter'] = array(
    '#type' => 'select',
    '#title' => 'Filter example',
    '#options' => drupal_map_assoc(array('Option 1', 'Option 2', 'Option 3', 'Option 4', 'Option 5')),
  );

  $form['actions'] = array(
    '#type' => 'actions',
  );
  
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'submit',
  );  
  
  return $form;
}

/**
 * MYFORM submit handler.
 */
function MYFORM_form_submit($node, &$form_state) {
  // Rebuild the form in order to keep filter values.
  $form_state['rebuild'] = TRUE;
}

If you're not sure how to build a form in Drupal 7, you can read the Create a form article.