Display submitted form data after redirecting to another page

The submitted form data will be lost after redirecting, so we’ll use PHP sessions to save the data so that we can access it on the next page.

Step 1

Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).

1
2
3
45
6
function my_save_form_data($form)
{
    session_start();
    $_SESSION['iphorm_2'] = $form->getValues();}
add_action('iphorm_post_process_2', 'my_save_form_data');
function my_save_form_data($form)
{
    session_start();
    $_SESSION['iphorm_2'] = $form->getValues();
}
add_action('iphorm_post_process_2', 'my_save_form_data');
  • On lines 4 and 6, change the number 2 to your form ID

Step 2

We need to execute PHP code to print the saved variables on the page we redirect to. One way to be able to do this is to create a Page Template. To do this, copy the “page.php” file from your theme and call it template-thank-you.php

At the top of the file template-thank-you.php, after the opening PHP tag, add the comment below to register the page template with WordPress.

1
2
3
4
/**
 * Template Name: Thank You
 */
session_start();
/**
 * Template Name: Thank You
 */
session_start();

Remove the “loop” code from the file (see the Sample template-thank-you.php section below) and write your message HTML instead. You can print the submitted form data using the code below.

1
<?php echo esc_html($_SESSION['iphorm_2']['iphorm_2_1']); ?>
<?php echo esc_html($_SESSION['iphorm_2']['iphorm_2_1']); ?>
  • Replace the number 2 in iphorm_2 with the form ID.
  • Replace iphorm_2_1 with the unique element ID of the element that you want to print the value for.

To print another value you can use the same code, just change the unique element ID.

Note: some form elements are not able to be printed directly as they return an array, see the Getting form values guide for the data types of all elements. You may need a PHP developer if you get stuck.

Step 3

Create or edit a page inside WordPress to be your destination page, on the right hand side at the Page Attributes box, for Template choose Thank You and save the page.

Step 4

Set up the form to redirect to this page by going to Form Builder → Settings → General → Successful submit options, at On successful submit choose Redirect to another page, at Redirect to choose Page and select the destination page created in Step 3.

Sample template-thank-you.php

The following file was created by following the above guide on the TwentyTwelve theme, it will possibly not work with your theme, but it will show you how to modify the file. If you compare this to the default page.php file of the TwentyTwelve theme, you’ll be able to figure out what code to change.

<?php
/**
 * Template Name: Thank You
 */
session_start();
get_header(); ?>

	<div id="primary" class="site-content">
		<div id="content" role="main">

			Thank you <?php echo esc_html($_SESSION['iphorm_2']['iphorm_2_1']); ?>, your message has been sent!

		</div><!-- #content -->
	</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Still having trouble? Head over to the forums.

Forums