I've been looking for an answer to this for hours now.

A) No matter what I do, the form will always be visible again after submit. How can I stop the from from displaying after a successful submit?

B) If there are validation errors, the form will attach to the existing form, resulting in 2,3,4,5,.. forms on the page. How to stop that?

This is the code:

function agals_support_form($form, &$form_state) {
$form['wrapper'] = array("#markup" => "<div id='status'></div>");

$form['name'] = array(
  '#type' => 'textfield',
  '#title' => t('Name'),
  '#default_value' => '',
  '#maxlength' => '128',
  '#required' => TRUE,

$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('submit'),
  '#ajax' => array(
    'callback' => 'agals_support_form_ajax_handler',
    'effect' => 'fade',
    'wrapper' => 'status',

if(isset($form_state['values']['name'])) { //output 
  $form['form_output'] = array(
    '#prefix' => '<p>',
    '#suffix' => '</p>',
    '#markup' => t('You entered @value', array('@value' => $form_state['values']['name'])),
} } return $form; }

function agals_support_form_submit(&$form, &$form_state) {
  watchdog('agals_support', 'submitted');  
  drupal_set_message('Form submitted');
  $form_state['rebuild'] = TRUE;

function agals_support_form_validate(&$form, &$form_state) {
  watchdog('agals_support', 'validation');
  if (empty($form_state['values']['name'])) {
    watchdog('agals_support', 'name missing');  
    form_set_error('name', t('Please enter your name.'));

function agals_support_form_ajax_handler(&$form, &$form_state) {
  watchdog('agals_support', 'ajax_handler');
  return $form;
  • If we first have to read through your code to guess, what you want to achieve, your question is not very likely to receive a good answer or risks being closed for being too broad/unclear. I think to have a good guess of your goals/intentions, but please be so kind to add it to your question. It's always a good idea to structure questions on Drupal Answers like "I want to achieve ..., I already did ... and expected ..., but received .... What did I do wrong?" This enables for good and to the point answers. - drupal.stackexchange.com/help/how-to-ask – Mario Steinitz Mar 2 '19 at 13:46

The issue you are facing is that you are not wrapping your form with the wrapper div, you're prepending a div to your form. When the ajax form is loaded, it is inserted into this wrapper div, rather than replacing the contents of the form.

Change this:

$form['wrapper'] = array("#markup" => "<div id='status'></div>");

To this:

$form["#prefix"] = "<div id='status'>";
$form["#suffix"] => "</div>";

This will wrap your form in the #status div.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.