无法从Contact Form 7表单发送WordPress外的POST值
问题描述:
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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
1 2 3 4 5 6 7 8 |
<?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
1 2 3 4 |
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.
图片转代码服务由CSDN问答提供
感谢您的意见,我们尽快改进~
功能建议
我的目标是通过联系表单7的POST将值提交到重定向的外部php页面并回显这些值。 </ p>
问题是在提交后重定向页面时,除非我删除JS并更改表单的操作Url,最后删除所有联系表单,否则值似乎丢失了 我需要的功能(验证,发送电子邮件等)。</ p>
我尝试了多个插件,例如联系表格7到API,它只能通过我不想要的GET方法工作 。 我试图获取发布的值并将它们转换为会话但它不起作用,因为我可能做错了。</ p>
这是表格:
在此处输入图像说明 </ p>
以下是显示的结果:\\ n 在此处输入图像说明 </ p>
更新:</ strong> </ p>
好的,所以我发布了获取发布数据的函数和结果php文件中缺少的var_dump。</ p>
这是fucntions.php </ p>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
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> <p>这是show-values.php </ p> \\ ñ<pre> <代码>&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> <p>这里是JS我在wordpress的编辑页面上进行重定向</ p> \\ n <pre> <code> document.addEventListener(\'wpcf7mailsent\',function(event){ location =\'https://www.xxxxx.xxx/forms/show-values.php\'; },false) ; </ code> </ pre> <p>输出仍然缺失: <a href>在此输入图像说明</a> </ p> <p>目前除了cf7之外没有使用任何额外的插件。</ p> </ div> <h5>网友观点:</h5> <div> <p>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:</p> <ol> <li>Transfer data as GET parameters. Simplest but not what you want.</li> <li>Use Javascript to submit form again after first submit but this time to needed URL.</li> <li>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 <code>session_start()</code>? 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.</li> </ol> </div> ### <div> <p>Try doing this instead of redirecting to your code:</p> <pre><code>funtionThatCallsMyHandler() { //yourcode } |
Put that in functions.php then in the spot you\'re calling your redirect code instead do:
1 2 3 4 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
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.
1 2 3 4 5 6 7 |
$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:
1 2 3 4 |
function what_I_wanna_do { // stuff } |
Then you declare a trigger that calls the function:
1 2 |
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).
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » 无法从Contact Form 7表单发送WordPress外的POST值