Home » Php » php – How to capture POST data with contact form7

php – How to capture POST data with contact form7

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have this hook in my functions.php:

add_action( 'wpcf7_mail_sent', 'myfunction' );

I want to post the values when the form is sent.

I have a field like this: [textarea your-message].

How do i capture the POST data from this?

for example when the form is sent i want to echo the post value of [textarea your-message] in myfunction(){}

How to&Answers:

You need to access the $WPCF7_ContactForm object.

In your hooked function, you’d access the field you want like this:

yourFunction(&$WPCF7_ContactForm) {
    $text_area_contents = $WPCF7_ContactForm->posted_data['your-message'];
}

Answer:

This is how I used and its work for me to receive contact form 7 data after successe mail send and I used this data to send another server through API

add_action( 'wpcf7_mail_sent', 'your_wpcf7_mail_sent_function' ); 
function your_wpcf7_mail_sent_function( $contact_form ) {
    $title = $contact_form->title;
    $submission = WPCF7_Submission::get_instance();  
    if ( $submission ) {
        $posted_data = $submission->get_posted_data();
    }       
   if ( 'Reagistation' == $title ) {
        $name = strtolower($posted_data['text-name']);
        $name = strtolower(str_replace(' ', '_',  $name));
        $email = strtolower($posted_data['email']);
        $phone = strtolower($posted_data['phone']);
        $Areyouarealtor = $posted_data['Areyouarealtor'];
        $ayor = strtolower($Areyouarealtor['0']);

 }
}

Answer:

Try this :

add_action( 'wpcf7_sent', 'your_wpcf7_function' ); 

function your_wpcf7_function( $contact_form ) {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();

if ( $submission ) {
    $posted_data = $submission->get_posted_data();
}
   if ( 'MyContactForm' == $title ) {

    $firstName = $posted_data['first-name'];
    $lastName = $posted_data['last-name'];


   }
}

Answer:

You can retrieve form fields data through wpcf7_posted_data

add_filter( 'wpcf7_posted_data', function( $data ) {
  echo $data['your-name'];
} );  

Answer:

Try This


add_action('wpcf7_before_send_mail','dynamic_addcc');
function dynamic_addcc($WPCF7_ContactForm){
    $currentformInstance  = WPCF7_ContactForm::get_current();
    $contactformsubmition = WPCF7_Submission::get_instance();
    if($contactformsubmition){
    $posted_data = $contactformsubmition->get_posted_data(); 
    }
}