Christian Mayne Christian Mayne - 6 months ago 66
Javascript Question

Sending variables from Wordpress Contact Form 7 submission to success page

I am using Contact Form 7 with Wordpress 3.5.

Currently, when a user submits the message, they are redirected to a success page by using the following in the "Additional Settings" field:

on_sent_ok: 'location.replace("http://www.example.org/success-page");'


I want to be able to customise the output of the success-page by using the input from a field, for example:

on_sent_ok: 'location.replace("http://www.example.org/success-page?name=yourname");'


I hoped that by dropping the usual Contact Form 7 shortcodes into the Additional settings, it may have sent the field value with it, but that's not the case.

Can anyone suggest how I can get the field values from contact form 7 into the url, or alternatively send as a $_POST parameter? It may require some javascript to do this, I guess.

Answer

This is possible but you need the save the posted data from the contact form to the session and show it there.

Add this to your functions.php

add_action('wpcf7_mail_sent', 'save_cf7_data');


function save_cf7_data($cf) 
{

    if(session_id() == '') {
       session_start();
    }

    $current_submission = WPCF7_Submission::get_instance();

    $_SESSION['cf7_submission'] = $current_submission->get_posted_data();


}

And your success page you just need to print the session var, like:

echo $_SESSION['cf7_submission']['name'];

That's all.