简体   繁体   中英

Contact Form 7 Conditional value based on drop-down without jquery

I am trying to get the drop down option value to the function. based on the drop down value it has to send the email. The below switch case code is working for the text box but not in the drop down(select) . We have to do more processes in this form so that we avoid contact form 7 pipes option.

View Code

<span class="wpcf7-form-control-wrap location">
<select name="location" class="wpcf7-form-control wpcf7-select wpcf7-validates-as-required" aria-required="true" aria-invalid="false">
<option value="">Select Location</option>
<option value="India">India</option>
<option value="UK & Europe">UK & Europe</option>
<option value="Americas">Americas</option>
<option value="Asia Pasific">Asia Pasific</option>
<option value="Oceania">Oceania</option>
</select>
</span>

Function Code

 add_action( 'wpcf7_before_send_mail', 'wpcf7_do_something_else_with_the_data', 90, 1 );
        
        function wpcf7_do_something_else_with_the_data( $WPCF7_ContactForm ){
         $submission = WPCF7_Submission :: get_instance();
        
            if ( $submission ){
                $posted_data = $submission->get_posted_data();      
                if ( empty( $posted_data ) ){ return; }
                
                
                $favcolor = $posted_data['location'];
    
                switch ($favcolor) {
                  case "India":
                    $changed_name = 'ddd@gmail.com';
                    break;
    
                   case "UK & Europe":
                    $changed_name = 'eee@gmail.com';
                    break;
    
                    case "Americas":
                        $changed_name = 'fff@gmail.com';
                        break;
    
                  case "Asia Pasific":
                    $changed_name = 'ggg@gmail.com';
                    break;
                  default:
                  $changed_name = 'hhh@gmail.com';
                  
                }
    
                $mail = $WPCF7_ContactForm->prop( 'mail' );
                
                $new_mail = str_replace( '[recipients]', $changed_name, $mail );
                // Set
                $WPCF7_ContactForm->set_properties( array( 'mail' => $new_mail ) );
                
                return $WPCF7_ContactForm;
            }}

At some point between 5.1 and 5.2, the developer of CF7 decided that selects were returned as arrays. It's kind of a pain, since it caused me a few headaches updating sites.

But opinions aside...

Try changing

$favcolor = $posted_data['location'];

to

$favcolor = $posted_data['location'][0];

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