Custom coding: Advanced Webform Confirmation Pages

Last updated on
30 April 2025

In 3.x and newer versions of Webform, the use of PHP validation and processing is deprecated because it's unsafe, but you can do whatever you need to within a custom module. Here are the most important parts you'll need for your module to communicate with your webform and validate or process the data available to the webform.

In this example, I have a webform that allows visitors to register for an event. After the user submits the form, they receive a confirmation page that uses some of the submitted data. It prints an ID and sends an email with the same information. I'll be using the 7.x version for this example but if you know how to code, you'll have no problems downgrading to 6.x-3.x

The first step is to create a hook_menu with the callback to hold the confirmation page:

<?php
function reg_menu() {
  	$items['register/confirmation/%/%'] = array(
		'page callback' => 'reg_confirmation_page',
		'page arguments' => array(2, 3),
		'access arguments' => array("access content"),
		'type' => MENU_CALLBACK,
		'title' => 'Confirmation',
	);

        return $items;
}

?>

This is pretty straight forward, all you need to do now is go to your webform configuration form and set that you want to redirect the user to a new page after form submission. The page you'll need to enter is: register/confirmation/%nid/%sid

The nid and sid values will be replaced by webform and our module will recognize them.

Now create the page callback 'reg_confirmation_page'.

<?php
// The $nid is not actually used here, but I'm adding it in case you need to get some data from the webform node.
function reg_confirmation_page($nid, $sid) {
        // Get the submission array
	$submission = webform_menu_submission_load($sid, $nid);
	
	// Process the submission
	$data = reg_confirmation_process_submission($submission);
	
	// I'm adding some CSS to theme the confirmation page.
	drupal_add_css(drupal_get_path("module", "reg") . "/reg.css", array('media' => 'screen'));
	drupal_add_css(drupal_get_path("module", "reg") . "/reg-print.css", array('media' => 'print'));
	
	// Process the data throught the theme layer
	$output = theme("confirmation_page", array('data' => $data));
	
	// Send the mail
	reg_send_email("confirmation", $data);
	
	return $output;
}
?>

As you can see, after I get the $submission from the 'sid', I send the data through another function to process the data. What does it mean? It means that webform presents the data array in a not very friendly way, so we need to simplify this for the rest of our module.

<?php
function reg_confirmation_process_submission(&$submission) {
	if($submission) {
		$data = array(
			'sid' => $submission->sid,
			'last_name' => $submission->data[1]['value'][0],
			'name' => $submission->data[3]['value'][0],
			'email' => $submission->data[4]['value'][0],
		);
		
		return $data;
	}
}
?>

We just create a $data array using the information of the $submission object. Remember that this object will be built when you add fields to your form, but in a general way, you'll have something like this:

$submission->data[foo]['value'][bar]

$submission is the object
->data is the array of arrays that holds the data provided by the user
[foo] this is a number that webform assigns when you add this field to the form. This number is not affected if you arrange the order of the fields at your form.
['value'][bar] if you use multiple values, you'll end up with an array that you'll have to process in some way. All my values are single so I'll always have ['value'][0]

One last thing. The reg_send_mail() function is a function I'm using to send an email. I won't explain it here, the only thing I need to mention is that you can send an email and because I'm passing the $data array to this function also, I can use all the information in the array when I send the email.

Help improve this page

Page status: Not set

You can: