How to show form entry data on a 'thank you' page after form submission

Created by Phil Kurth, Modified on Wed, 6 Jul, 2022 at 9:18 AM by Phil Kurth

If you are choosing to redirect the user after form submission to a custom page, you may need to display some or all of the submitted data to the user. Advanced Forms temporarily stores the submission long enough for you to do this using the af_get_field() function on the redirect page. 


The following example demonstrates how to register a custom shortcode to use on a 'thank you' page. You could also write the code within the shortcode handler function directly inside a custom template, if that is your preferred method. 


<?php

/**
 * [afp_demo_entry_summary] Shortcode.
 * This renders a summary of a form submission, if a submission is available.
 * This code can go in your functions.php file, a custom plugin, or even a snippet manager.
 */
add_shortcode( 'afp_demo_entry_summary', function ( $atts, $content = null ) {
  // Check if there is a submission available. If not, return a message.
  if ( ! af_has_submission() ) {
    return 'No entry to show.';
  }

  // Start PHPs output buffer so we can capture the proceeding markup and return it.
  ob_start();

  // Build the markup using AFs helper function to get the field data from the submission.
  ?>
  <table>
    <tr>
      <td>Name:</td>
      <td><?= af_get_field( 'name' ) ?></td>
    </tr>
    <tr>
      <td>Email:</td>
      <td><?= af_get_field( 'email' ) ?></td>
    </tr>
  </table>
  <?php
  
  // Get the content of the buffer and return it for display.
  return ob_get_clean();
} );


The 'thank you' page is set up within nothing more than a title and the [afp_demo_entry_summary] shortcode:



By setting up the form to redirect to our custom 'thank you' page on submission, the user will be presented with a summary of the information coded into the shortcode. 


Some common issues you might experience


Form entry data is not showing


If you set up your form to redirect to /thank-you-page, you might find WordPress adds a canonical redirect to /thank-you-page/ (note the trailing slash). This results in two redirects on form submission. Advanced Forms only maintains the temporary data for one page load so it won't be available after this second redirect. To fix this, ensure you include the trailing slash when configuring the form's redirect URL


Form entry data is from a different entry or is still not showing


If you have a page cache set up, you may be seeing a cached version of the page. This could be a cached version of a different entry or it could just be the cached version of the 'no entry found' fallback text. Disabling the page cache for the thank you page should fix this.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article