Providing Confirmation to a Form Submit in Drupal

In Drupal forms are created with the Form API. Forms contain items, and typically Submit buttons to send data back to the server. Sometimes after validation and we can to confirm an action. How can we confirm we want our action to occur? Without writing a ton of code?

Solution:

The Drupal 7/8 API contains a confirm_form function. When you need to show a confirmation page, and proceed or return you can make use of this function. Its function signature is:

confirm_form($form, $question, $path, $description = NULL, $yes = NULL, $no = NULL, $name = ‘confirm’)

It’s a fairly reusable — 2 option confirmation.

Here is an example implementation of a simple FAPI based form function that attempts to submit some data:

/* a form builder function */
function mymodule_custom_form($form_id, &$form_state, $args) {

  if (!isset($form_state['storage']['confirm'])) {
    $form = array();
    $form = // ... oringinal form content, like a login form.
    //
    // Add extra form elements here, such as to our custom
    // submit and validation handlers.
    return $form;
  }
  else
  {

    return confirm_form(
        array(),
        'Are you sure you want to submit this form?',
        'my_module/form',
        "You are about to submit your answers. Are you sure you want to submit this data?",
        'Yes, Submit the Data',
        'No, discard my answers and Start Over');
  }
}

/** Typical drupal submit function for form. */
function mymodule_custom_form_submit($form, &$form_state) {
  if (!isset($form_state['storage']['confirm'])) {
    // This will cause the form to be rebuilt, entering the confirm part of the
    // form.
    $form_state['storage']['confirm'] = TRUE;
    $form_state['storage']['original_form'] = $form_state['values'];
    $form_state['rebuild'] = TRUE;
  }
  else {
    // do real form submit of orginal data using
    // saved off $form_state['storage']['original_form'].
    drupal_goto('<front>');
  }
  }
}

On submit, the form goes through validation and during its rebuild process the form function notices that the confirmation form should be shown — the form builder function then builds and shows the confirmation form.

Note: That the confirmation form may take a $form array itself to show users another small form as needed. I typically don’t use this but it could be useful to use for a 3-option form: yes, no, take me elsewhere; as an example.

Looking for quality Drupal Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under Drupal | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at david.gurba@arvixe.com

4 Comments on Providing Confirmation to a Form Submit in Drupal

  1. Thierry says:

    Hi David,
    I found your article really interesting. The explantations are very clear.
    I tried your code and finally have a question. How could I if I want to have a confirm page showing all the values of the user ?
    Indeed, the same page but with a the user’s entries.

    Hope to read soon.
    Thank you very much for your work.

    Best regards,

    Thierry

    • David Gurba David Gurba says:

      Hi — the default confirm_form() for drupal is it’s own page (not the same page you filled out the form on). This is done for accessability reasons.

      To answer your question you can view the $form_state of the original form and output the values to the user as the message supplied to confirm_form(). Or, confirm_form() body can in fact hold another $form if you want. You could have this form have its own validation, submission or show anything you want — like the original $form_state as well.

      Let me know if you need more assistance, or post another specific question in the Drupal Arvixe Forum.
      regards,
      -dg

  2. Thierry says:

    Hi David,

    I follow year advise and it was very helpful. I put my the user’s values in the message field for the confirm form and it works. Thanks a lot.

    Another question, is it possible to do the same think but with a redirection on a specific url ? I know it’s possible with hook_menu() but with 1 or 2 wildcards. Could I do the same but with my entire $form_state ?

    If i’m not wrong, we lost values when we do a redirection.

    Thanks

    • David Gurba David Gurba says:

      It sounds like you want to show a confirmation form — then with like YES, redirect the whole $form_state to another page. Right now it sounds like your showing the form_state in the confirmation body.

      You have 2 options:

      (1) embed a new $form in the cofirmation page. Right now the 1st parameter of confirm_form() is an array(). It could be a custom $form you generate such as:


      $form = array();
      $form['preview_your_answers'] = array('#markup' => 'You supplied: '. print_r($form_state['storage']['original_form'], TRUE));
      $form['proceed_to_redirect_page'] = array('#type' => 'submit', '#value' => 'Yes, Go to Other page');
      $form['action'] = 'path/to/redirect/to';
      // ...
      confirm_form($form,
      'Are you sure you want to submit this form?',
      'my_module/form',
      "You are about to submit your answers. Are you sure you want to submit this data?",
      '',
      'No, discard my answers and Start Over');

      Use CSS to cleanup its presentation. The new form above has an action set to the custom URL you want to redirect to — the form_state values will simply go there. This form will also need its own validation and submit handlers.

      Option (2):

      dont change much of the example code I put in the blog post. Simply add $form_state['redirect'] = ‘path/to/redirect/to’; to your original form (not the confirmation form) and in the submit handler save off the $form_state['values'] to the users $_SESSION data, eg $_SESSION['temp_submitted_form'], on the redirected page simply look for the $_SESSION values.

      If you have further questions, I recommend creating some questions in the Drupal Arvixe forum. As comments aren’t really a good place for long questions such as this.

Leave a Reply

Your email address will not be published. Required fields are marked *


9 + 4 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>