【问题标题】:How to pass the fields values of a form submitted through CF7 on wordpress?如何在wordpress上传递通过CF7提交的表单的字段值?
【发布时间】:2019-10-23 13:44:20
【问题描述】:

我是 WordPress 爱好者,我正在努力完成一些事情,但我还没有找到方法。

这是交易,我有一个使用 Contact Form 7 制作的表单,我想在提交表单时提取值并在 WordPress 页面的正文中使用它们作为我在提交表单时运行的脚本.

在我的functions.php上我有:

add_action('wpcf7_mail_sent','save_data_in_variables');
add_action('wpcf7_mail_failed','save_data_in_variables');

function save_data_in_variables($conference_form){
$submission = WPCF7_Submission::get_instance();
if (!$submission){
    echo 'Form was not sent';
}
$posted_data = $submission->get_posted_data();
}

function wpc_vc_shortcode( $atts ) {
echo $posted_data['your-name'];
}
add_shortcode( 'name_in_the_form', 'wpc_vc_shortcode');

在页面的正文上,我运行了一个脚本,该脚本在提交表单时提交数据,但我想使用简码将值替换为表单的值:

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
truestats.logEvent('Lead Conference', { id: '[name-in-the-form]' } );
}, false );
</script>

我该如何做到这一点?到目前为止,当我提交表单时没有显示任何值。

非常感谢

【问题讨论】:

    标签: php wordpress function contact-form-7


    【解决方案1】:

    您可以在 javascript 中获取发布的表单数据,无需创建短代码并将其传递给 javascript。考虑下面的例子

    <script>
        document.addEventListener( 'wpcf7submit', function( event ) {
            if(event.detail.contactFormId == 'CONTACT_FORM_ID'){   // condition for specific form ,replace "CONTACT_FORM_ID" 
                                                                   // with form id you want to use this code for 
                var inputs = event.detail.inputs;
                for ( var i = 0; i < inputs.length; i++ ) {
                    if ( 'your-name' == inputs[i].name ) {
                        var name = inputs[i].value;
                        break;
                    }
                }
                truestats.logEvent('Lead Conference', { id:  name } );
            }
        }, false );
    </script>
    

    我使用的是 'wpcf7submit' 事件而不是 'wpcf7mailsent' 事件,您可以根据需要使用。

    您可以查看联系表 7 提供的DOM Events 以供进一步参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      • 2021-09-01
      相关资源
      最近更新 更多