Products
GG网络技术分享 2025-03-18 16:12 11
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 hereHere\'s the result that is supose to be shown:
enter image description hereUPDATE:
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
<?phpvar_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 hereNot using any extra plugin besides cf7 at the moment.
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议我的目标是通过联系表单7的POST将值提交到重定向的外部php页面并回显这些值。 </ p>
问题是在提交后重定向页面时,除非我删除JS并更改表单的操作Url,最后删除所有联系表单,否则值似乎丢失了 我需要的功能(验证,发送电子邮件等)。</ p>
我尝试了多个插件,例如联系表格7到API,它只能通过我不想要的GET方法工作 。 我试图获取发布的值并将它们转换为会话但它不起作用,因为我可能做错了。</ p>
这是表格:
在此处输入图像说明 </ p>
以下是显示的结果:\\ n 在此处输入图像说明 </ p>
更新:</ strong> </ p>
好的,所以我发布了获取发布数据的函数和结果php文件中缺少的var_dump。</ p>
这是fucntions.php </ p>
add_action(\'wpcf7_before_send_mail\',\'send_post_values\'); if($ form_id == 7627){
$ user = $ cf7-&gt; posting_data [“user”];
$ email = $ cf7-&gt; posting_data [“email”];
$ country = $ cf7-&gt; posting_data [“country”];
//任何代码丢失??
}
}
</ code> </ pre>
这是show-values.php </ p>
\\ ñ
<代码>&LT; PHP中var_dump($ _ POST); \\ NP rint_r($ _ POST);
echo“Name:”。 $ _POST [“用户”]。 “&LT峰; br&gt;” 中;
echo“电子邮件:”。 $ _POST [“email”]。 “&lt; br&gt;”;
echo“国家:”。 $ _POST [“country”]。 “&lt; br&gt;”;
?&gt;
</ code> </ pre>
这里是JS我在wordpress的编辑页面上进行重定向</ p>
\\ n
document.addEventListener(\'wpcf7mailsent\',function(event){location =\'https://www.xxxxx.xxx/forms/show-values.php\';
},false) ;
</ code> </ pre>
输出仍然缺失:
在此输入图像说明 </ p>
目前除了cf7之外没有使用任何额外的插件。</ p>
</ div>
网友观点:
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:
- Transfer data as GET parameters. Simplest but not what you want.
- Use Javascript to submit form again after first submit but this time to needed URL.
- 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.
###
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.
###
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.
###
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 exitedirect 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).
Demand feedback