简体   繁体   English

联系表格 7 提交后无法执行脚本

[英]Unable to execute script after contact form 7 submit

When I execute this code it doesn't do anything except there comes up a spinning icon underneath the contact form which spins forever.当我执行此代码时,它不会执行任何操作,只是在联系表单下方出现一个旋转图标,该图标将永远旋转。

add_action( 'wpcf7_before_send_mail', 'process_contact_form_data' );

function process_contact_form_data( $contact_data ){
    var_dump($contact_data->posted_data);
    $name = $contact_data->posted_data["your-name"];
    $email = $contact_data->posted_data["your-email"];

    echo $name ;
    echo $email;                
}

You can't echo the output of wpcf7_before_send_mail because there's no place to echo it to.您无法回显wpcf7_before_send_mail的输出,因为没有地方可以回显它。 The form process is all ajax.表单处理全是ajax。

You can however output it to the error_log or to a file.但是,您可以将其输出到 error_log 或文件。 This is an example of outputting the form data to the error_log.这是将表单数据输出到 error_log 的示例。

add_action('wpcf7_before_send_mail', 'output_cf7_form_data');
function output_cf7_form_data(){
    // Call the form data from the static instance of the class
    $submission = WPCF7_Submission::get_instance();

    if ( $submission ) {
        // assign the posted data to an array
        $posted_data = $submission->get_posted_data();
        $name = $posted_data["your-name"];
    }
    // Use Output Buffering to print_r form data to the error log
    ob_start();
    print_r($posted_data);
    echo 'Posted Name is ' . $name;
    $body = ob_get_clean();
    error_log($body);
}

If you were so inclined, you could change the part about putting it to the error log, and use fwrite to post the information to a file.如果您愿意,可以更改将其放入错误日志的部分,并使用fwrite将信息发布到文件中。

If you want to look at this Contact Form 7 to Constant Contact API method I used to work with the constant contact API, you can see how I use before send mail to capture the form data, but push to the API after wpcf7_mail_sent is completed so that the form submission isn't waiting for the API call to finish, and the user doesn't see the little ajax spinner while the API call happens.如果你想看看这个Contact Form 7 to Constant Contact API方法我曾经使用constant contact API,你可以看到我是如何在发送邮件之前使用来捕获表单数据,但是在wpcf7_mail_sent完成后推送到API所以表单提交不会等待 API 调用完成,并且用户在 API 调用发生时看不到小 ajax 微调器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM