RSS

Newsletter

The CodeKarate Newsletter is the best way for technical ninjas to keep their swords sharpened. Don't worry, we won't flood your inbox and your email address will always remain private.
Back to Top

Drupal 6 confirm form example

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!

Discussions

1
Mario (not verified)

Thanks

Thank you very much!!

2
drupal (not verified)

easy guide

example was really easy and it can be implemented without any problem thanks for it

3
Anonymous

Thank you very much, it's the

Thank you very much, it's the best article I've read about the confirm form :)

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <blockquote> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <php>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.