John
John

Reputation: 15

Can't send POST values outside of Wordpress from Contact Form 7 Form

my objective is to submit values via POST of a contact form 7 to a redirected outside php page and echo those values.

The issue is when the page is redirected after the submission, the values seem to be lost unless i remove the JS and change the action Url of the form ending up removing all the contact form 7 features which i require (validation, email sent, etc..).

I tried multiple plugins such as contact form 7 to API and it only works via GET method which i don't want. I tried to get the posted values and transforming them into session but it's not working because i'm probably doing something wrong.

Here's the form: enter image description here

Here's the result that is supose to be shown: enter image description here

UPDATE:

Ok so i posted the function to fetch the posted data and the var_dump missing on the result php file.

Here's the fucntions.php

add_action( 'wpcf7_before_send_mail', 'send_post_values' );


  if ($form_id == 7627) {

    $user      = $cf7->posted_data["user"];
    $email = $cf7->posted_data["email"];
    $country  = $cf7->posted_data["country"];

    //any code missing??

  }

}

Here's the show-values.php

<?php
var_dump($_POST);
print_r($_POST);
echo "Name: " . $_POST["user"] . "<br>"; 
echo "Email: " . $_POST["email"] . "<br>";
echo "Country: " . $_POST["country"] . "<br>";
?>

And here's the JS i put on the edit page of wordpress for redirection

document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'https://www.xxxxx.xxx/forms/show-values.php';
}, false );

The output is still missing: enter image description here

Not using any extra plugin besides cf7 at the moment.

Upvotes: 1

Views: 3858

Answers (4)

Mahfuzul Hasan
Mahfuzul Hasan

Reputation: 172

You can use Contact Form 7 hook for this.

try this:

add_action( 'wpcf7_before_send_mail', 'do_your_task' );

function do_your_task($cf7) {
  $form_id = $cf7->id();

  // Do some task only for specific form
  if ($form_id == 123) { // 123 => Your Form ID.

    $email      = $cf7->posted_data["your-email"]; // Update "your-email" to the actual field name you have given in the form
    $first_name = $cf7->posted_data["your-firstname"];
    $last_name  = $cf7->posted_data["your-lastname"];


    //pass data to 3'rd party API

  }

}

See all the hooks provided by Contact Form 7

Update: As you updated the question. for your case you can try this: You can send values via query string via url or by creating a session variable.

Add this code in functions.php

add_action( 'wpcf7_before_send_mail', 'send_form_data' );

function send_form_data($cf7) {
  $form_id = $cf7->id();

  // Do some task only for specific form
  if ($form_id == 123) { // 123 => Your Form ID.    
    $posted_data_str = http_build_query( array("data" => $cf7->posted_data ) );
    $location =  "https://www.xxxxx.xxx/forms/show-values.php?" . $posted_data_str;
    wp_redirect( $location );
    die();
  }
}

now no need to redirect via js

in your show-values.php file add the following code.

$get_data = $_GET['data'];
if ( !empty($get_data)) {
  var_dump($get_data);

  echo "Name: " . $get_data["your-name"] . "<br>";  // Change "your-name"
}

However, another way is by creating a SESSION variable.

Upvotes: -1

Valdars
Valdars

Reputation: 863

Problem is that when redirecting request on browser side you are basically starting completely new request that will be GET request. Some of the things you can try:

  1. Transfer data as GET parameters. Simplest but not what you want.
  2. Use Javascript to submit form again after first submit but this time to needed URL.
  3. Save data after submit and retrieve it on correct page after redirect. Basically what you tried with session but you said it didn't work. Did you use session_start()? Also it only works if both pages are part of same web page. You can also try storing this data in file/database/somewhere else and maybe passing on some sort of id as GET parameter to find that data after redirect.

Upvotes: 0

Mason Stedman
Mason Stedman

Reputation: 615

The other answer here is basically right, it just assumes you know how wordpress works. There's a file called functions.php that's generally where you want to to add any php code to w\e your doing. How to interact with it is a two part thing, first you declare a function like normal:

function what_I_wanna_do {
    // stuff
}

Then you declare a trigger that calls the function:

add_action( 'wpcf7_before_send_mail', 'what_I_wanna_do' );

In that trigger the contact form plugin allows you to hook into it so you can fire something, in this case, before that plugins core functions occur. Basically that line says, before doing the contact form stuff, run function "what_I_wanna_do". Put all the stuff you want to happen in what_I_wanna_do, then exit\redirect however you want at the end. You can declare all these things in functions.php (or make your own plugin so it's easier to drop in and out of the code\update WP).

Upvotes: -1

Mason Stedman
Mason Stedman

Reputation: 615

Try doing this instead of redirecting to your code:

funtionThatCallsMyHandler() {
    //yourcode
}

Put that in functions.php then in the spot you're calling your redirect code instead do:

funtionThatCallsMyHandler();
redirect
exit

That way it still has the post data in the first place, vs chaining redirects.

Upvotes: -1

Related Questions