Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

Drupal 6 confirm form example

Parent Feed: 

There are many times when you will be developing a module that you will need to confirm an action. An example might include before deleting a piece of content, or changing the status of something. Here is a very simple example of how to create a confirmation form in a Drupal 6 module.

/**
 * Implements hook_menu().
 */
function MYMODULE_menu() {
  $items = array();
  $items['MYMODULE/do-something'] = array(
    'type' => MENU_CALLBACK,
    'access arguments' => array('access content'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('MYMODULE_confirm_form'),
  );
 
  return $items;
}
 
/**
 * Confirm form example
 */
function MYMODULE_confirm_form($form_state) {
 
  //you can pass variables here if need be
  $form['_my_var'] = array(
    '#type' => 'value',
    '#value' => 'MY Variable',
  );
 
  //you can set a redirect if needed
  $form['#redirect'] = 'user';
 
  return confirm_form($form,
    t('Are you sure you want to do something?'), //message title
    isset($_GET['destination']) ? $_GET['destination'] : 'user', //redirect if cancelled
    t('Only say yes if you are really sure.'), //message description
    t('Lets Do it!'), //confirm button text
    t('Cancel') //cancel button text
  );
}
 
/**
 * Submit handler for confirm form
 */
function MYMODULE_confirm_form_submit($form, &$form_state) {
	//verify that the form was confirmed
  if ($form_state['values']['confirm']) {
    //Do something here
 
    //set a message
    drupal_set_message('You did something!');
 
  }
}

You can do much more than this such as pass variables for user id's or node id's in the URL (through hook_menu). Hopefully this very simple example will get you started though. Its always good to make sure your users are confident in their actions before letting them do something dangerous.

Cheers!

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web