Telephone: 0844 967 0565 Email: info@pushon.co.uk

WordPress Contact Form 7 – Intelligent Thank you pages

I have been developing a site that required the “thank you” page after the user submits a form to be aware of the options submitted. “Easy!” you say, “just grab the data from the $_POST or $_GET array”.

Unfortunately this isn’t really an option in CF7, as the data is emailed out (or stored in the database depending how you have it set up) and then the user is shown a confirmation message. I used this method to get the user redirected to a thank you page, but by that point all the data from the form is no longer available.

My solution: grab the data as soon as it is submitted and store it in the user’s session. To do this I added an action (in my theme’s functions.php) to get fired when the ‘wpcf7_before_send_mail’ event happens, and store it in the database, like so:

function set_sessions($cf7){
$_SESSION['formdata'] = $cf7->posted_data['menu-848'];
return false;
}

add_action( ‘wpcf7_before_send_mail’, ‘set_sessions’ );

‘Menu-848′ is just the one piece of data I wanted to grab. If you want to store the whole set of data, just use

$_SESSION['formdata'] = $cf7->posted_data;

instead.

Once it’s stored, you just have to get it out of the session array wherever you want.

Hope this helps, let me know if you have any questions.

Edit: Just a note in case it’s needed – you should also ensure that you actually have a session open, as WordPress doesn’t do this automatically.

To do this, add the following to your wp_config file:

/**
* Enable sessions
*/
if (!session_id())
session_start();

To find out how PushON research & analysis services can give your company a competitive online edge, simply call 0844 967 0565 or contact us via our form.

  • Imkiss

    Hi.. This is exactly what I am looking for!

    I want to take a couple of form fields in CF7 (contact form 7) submitted on ONE form… and store them on submit to auto-populate the NEXT form in a couple of places.

    So when email and firstname is submitted…. it jumps to another form with more fields like address etc… but the email and first name is already filled out.

    I have the “CATCHING” form al ready to receive the variables from the GET string to auto populate…and it works…. but I can NOT (for the life of me!) figure out how to take those submitted vars and pass them into the GET string to that page!

    I tried for MANY hours to use Javacript in this way….
    on_sent_ok: “var email = document.getElementById(‘email’); location = ‘/application?email=’+email.value;”

    But it JUST DOESNT WORK… so I thought “maybe” I could store them in a session and retrieve them at the next page some how.

    It’s driving me crazy.

    Please advise if you can. I would be most grateful!

    • pushonltd

      Hi Imkiss, I think you’ve got the right idea with using javascript for this, I’d say you need to use the session to store the data, and then javascript to fill in the second form. Once you have it stored in the session try this code on the template file of your second form:

      $(function() {
      $(“input#yourinput”).val(“”);
      });

      If you are having trouble with that, this page might help: http://api.jquery.com/val/#val2

  • Imkiss

    Hi!

    Just a quick note to say after many hours… AND THANKS TO YOUR PAGE… I created my own modified solution using SESSIONS and it worked GREAT.

    Big thanks!

  • Guest

    Great Tip! Thank You!

  • arnold

    Hi, I’ve put this on my functions.php page on my template but I can’t seem to retrieve the values on the thank you page after the form is posted.

    Maybe it is my lack of PHP knowledge but how do I display the value of the session on my thank you page? E.g. if the textfield on the contact form is called “country”, how would I display this value on the thank you page after submission?

    • James Sims

      Hey Arnold, on your thank you page try this code: This should show you all the data that has been stored in the session. If it’s called country it would be something along the lines of $_SESSION['formdata']['country']. If you’re still stuck post back here and we will get it sorted out!

    • http://www.facebook.com/James.R.Sims James Sims

      Please see my edit above, you might need to initialise the session first.

  • Andrea Moro

    Hi there,

    thanks for your solution. However, I wonder if you can tell me what should I use instead of the ['menu-848'] as I’m sure this is specific for your form.

    Is that the form ID enclosed in brackets and visible only in the admin panel?

    Thanks for your answer.
    Andrea

    • http://www.facebook.com/James.R.Sims James Sims

      Hi Andrea, that’s right. An easy way to discover the keys for all the data you have is to put the whole posted_data array into the session, then do a print_r() of that somewhere else.

      You should also ensure that you actually have a session open, as WordPress doesn’t do this automatically.

      To do this, add the following to your wp_config file:

      /**
      * Enable sessions
      */
      if (!session_id())
      session_start();