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
Thanks
Thank you very much!!
easy guide
example was really easy and it can be implemented without any problem thanks for it
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