简体   繁体   中英

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. The form process is all ajax.

You can however output it to the error_log or to a file. This is an example of outputting the form data to the 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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