Title: Creating Multiple Conditional E-mail Forms
Last modified: August 21, 2016

---

# Creating Multiple Conditional E-mail Forms

 *  [mattyk1972](https://wordpress.org/support/users/mattyk1972/)
 * (@mattyk1972)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/creating-multiple-conditional-e-mail-forms/)
 * Hi Michael
 * I hope you’re well. I had a quick question that’s linked to a problem you helped
   me solve a few months ago. I was looking to create a form that would be submitted
   to various different e-mails depending on the options the user had selected. 
   I got the form set up and working great but I know need to create another form
   to do something similar on the same site.
 * I’ve had a look at the page you created that explains how to add the required
   filter ([http://cfdbplugin.com/?page_id=804](http://cfdbplugin.com/?page_id=804))
   but I’m not sure how much of the filter code needs to be added for the second
   conditional e-mail form. Do I need to add all the filter code again for the new
   form, or just add the new form’s details to the existing filter?
 * Your help would be appreciated.
 * Many thanks
 * Matt
 * [http://wordpress.org/extend/plugins/contact-form-7-to-database-extension/](http://wordpress.org/extend/plugins/contact-form-7-to-database-extension/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Author [Michael Simpson](https://wordpress.org/support/users/msimpson/)
 * (@msimpson)
 * [12 years, 12 months ago](https://wordpress.org/support/topic/creating-multiple-conditional-e-mail-forms/#post-3855078)
 * You could make a general PHP function with most of the code which you don’t need
   to change, then make a smaller function for each form. Like this for “Form 1”
   and “Form 2”:
 *     ```
       function location_form_handler($formData, $formName, $fieldName)
       {
           if ($formData && $formName == $formData->title && $formData->scanned_form_tags) {
               $emailSelected = $formData->posted_data[$fieldName];
               $valueSelected = null;
               foreach ($formData->scanned_form_tags as $tag) {
                   if ($tag['name'] == $fieldName) {
                       foreach ($tag['raw_values'] as $rawValue) {
                           // value|email
                           $valuesArray = explode('|', $rawValue);
                           if (count($valuesArray) == 2 && $valuesArray[1] == $emailSelected) {
                               $valueSelected = $valuesArray[0];
                               break;
                           }
                       }
                   }
                   if ($valueSelected != null) {
                       break;
                   }
               }
               if ($valueSelected != null) {
                   $formData->posted_data[$fieldName] = $valueSelected;
                   $formData->posted_data[$fieldName . '_email'] = $emailSelected;
               }
           }
           return $formData;
       }
   
       // Form 1
       function location_form_handler_form1($formData) {
           $formName = 'Form 1'; // change this to your form's name
           $fieldName = 'location'; // change this to your field's name
           location_form_handler($formData, $formName, $fieldName);
       }
       add_filter('cfdb_form_data', 'location_form_handler_form1');
   
       // Form 2
       function location_form_handler_form2($formData) {
           $formName = 'Form 2'; // change this to your form's name
           $fieldName = 'location'; // change this to your field's name
           location_form_handler($formData, $formName, $fieldName);
       }
       add_filter('cfdb_form_data', 'location_form_handler_form2');
       ```
   
 *  Thread Starter [mattyk1972](https://wordpress.org/support/users/mattyk1972/)
 * (@mattyk1972)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/creating-multiple-conditional-e-mail-forms/#post-3855215)
 * Hi Michael
 * Thanks for getting back to me so quickly. I tried the code you provided but for
   some reason it’s still not giving the results we’re looking for. We’re just getting
   the e-mail address we’ve added after the pipes instead of the field value.
 * I tried changing the existing code we had for the original conditional e-mail
   form, and adding in the values of the new, second form, and that seemed to work
   fine, so I don’t think there’s an issue with the new form itself. I’ve attached
   the original code I was using which seems to be working fine (for either conditional
   e-mail form):
 *     ```
       function location_form_handler($formData)
       {
           $formName = 'Brochure Request Form'; // change this to your form's name
           $fieldName = 'country'; // change this to your field's name
           if ($formData && $formName == $formData->title && $formData->scanned_form_tags) {
               $emailSelected = $formData->posted_data[$fieldName];
               $valueSelected = null;
               foreach ($formData->scanned_form_tags as $tag) {
                   if ($tag['name'] == $fieldName) {
                       foreach ($tag['raw_values'] as $rawValue) {
                           // value|email
                           $valuesArray = explode('|', $rawValue);
                           if (count($valuesArray) == 2 && $valuesArray[1] == $emailSelected) {
                               $valueSelected = $valuesArray[0];
                               break;
                           }
                       }
                   }
                   if ($valueSelected != null) {
                       break;
                   }
               }
               if ($valueSelected != null) {
                   $formData->posted_data[$fieldName] = $valueSelected;
                   $formData->posted_data[$fieldName . '_email'] = $emailSelected;
               }
           }
           return $formData;
       }
       add_filter('cfdb_form_data', 'location_form_handler');
       ```
   
 * And here is the new code I tried to add to replace it, I’m not sure if there’s
   something in there that I’ve missed out but the field we used the pipes in always
   comes back as the e-mail address, rather the value the user selected. I even 
   tried changing the field names so they were different n case that was an issue,
   but it still gave the same results.
 *     ```
       function location_form_handler($formData, $formName, $fieldName)
       {
           if ($formData && $formName == $formData->title && $formData->scanned_form_tags) {
               $emailSelected = $formData->posted_data[$fieldName];
               $valueSelected = null;
               foreach ($formData->scanned_form_tags as $tag) {
                   if ($tag['name'] == $fieldName) {
                       foreach ($tag['raw_values'] as $rawValue) {
                           // value|email
                           $valuesArray = explode('|', $rawValue);
                           if (count($valuesArray) == 2 && $valuesArray[1] == $emailSelected) {
                               $valueSelected = $valuesArray[0];
                               break;
                           }
                       }
                   }
                   if ($valueSelected != null) {
                       break;
                   }
               }
               if ($valueSelected != null) {
                   $formData->posted_data[$fieldName] = $valueSelected;
                   $formData->posted_data[$fieldName . '_email'] = $emailSelected;
               }
           }
           return $formData;
       }
   
       // Form 1
       function location_form_handler_form1($formData) {
           $formName = 'Brochure Request Form'; // change this to your form's name
           $fieldName = 'country'; // change this to your field's name
           location_form_handler($formData, $formName, $fieldName);
       }
       add_filter('cfdb_form_data', 'location_form_handler_form1');
   
       // Form 2
       function location_form_handler_form2($formData) {
           $formName = 'USA Booking Form'; // change this to your form's name
           $fieldName = 'country'; // change this to your field's name
           location_form_handler($formData, $formName, $fieldName);
       }
       add_filter('cfdb_form_data', 'location_form_handler_form2');
       ```
   
 * Your patience and help is really appreciated.
 * Thanks
 * Matt
 *  Plugin Author [Michael Simpson](https://wordpress.org/support/users/msimpson/)
 * (@msimpson)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/creating-multiple-conditional-e-mail-forms/#post-3855273)
 * Sounds like the filter is not being called. You might try following the debug
   instruction on that web page. Put error_log statements in the location_form_handler1
   and location_form_handler2 to make sure they are called.
 *  Thread Starter [mattyk1972](https://wordpress.org/support/users/mattyk1972/)
 * (@mattyk1972)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/creating-multiple-conditional-e-mail-forms/#post-3855279)
 * Hi Michael
 * I’ve always struggled with the debugging process so I had another look at the
   code and seem to have got it to work by adding two bits of code – see below:
 *     ```
       function location_form_handler($formData)
       {
           $formName = 'Brochure Request Form'; // change this to your form's name
           $fieldName = 'country'; // change this to your field's name
           if ($formData && $formName == $formData->title && $formData->scanned_form_tags) {
               $emailSelected = $formData->posted_data[$fieldName];
               $valueSelected = null;
               foreach ($formData->scanned_form_tags as $tag) {
                   if ($tag['name'] == $fieldName) {
                       foreach ($tag['raw_values'] as $rawValue) {
                           // value|email
                           $valuesArray = explode('|', $rawValue);
                           if (count($valuesArray) == 2 && $valuesArray[1] == $emailSelected) {
                               $valueSelected = $valuesArray[0];
                               break;
                           }
                       }
                   }
                   if ($valueSelected != null) {
                       break;
                   }
               }
               if ($valueSelected != null) {
                   $formData->posted_data[$fieldName] = $valueSelected;
                   $formData->posted_data[$fieldName . '_email'] = $emailSelected;
               }
           }
           return $formData;
       }
       add_filter('cfdb_form_data', 'location_form_handler');
   
       function location_form_handler2($formData)
       {
           $formName = 'USA Booking Form'; // change this to your form's name
           $fieldName = 'country'; // change this to your field's name
           if ($formData && $formName == $formData->title && $formData->scanned_form_tags) {
               $emailSelected = $formData->posted_data[$fieldName];
               $valueSelected = null;
               foreach ($formData->scanned_form_tags as $tag) {
                   if ($tag['name'] == $fieldName) {
                       foreach ($tag['raw_values'] as $rawValue) {
                           // value|email
                           $valuesArray = explode('|', $rawValue);
                           if (count($valuesArray) == 2 && $valuesArray[1] == $emailSelected) {
                               $valueSelected = $valuesArray[0];
                               break;
                           }
                       }
                   }
                   if ($valueSelected != null) {
                       break;
                   }
               }
               if ($valueSelected != null) {
                   $formData->posted_data[$fieldName] = $valueSelected;
                   $formData->posted_data[$fieldName . '_email'] = $emailSelected;
               }
           }
           return $formData;
       }
       add_filter('cfdb_form_data', 'location_form_handler2');
       ```
   
 * Thanks for all your help.
 * Matt

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Creating Multiple Conditional E-mail Forms’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/contact-form-7-to-database-extension_ffffff.
   svg)
 * [Contact Form DB](https://wordpress.org/plugins/contact-form-7-to-database-extension/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/contact-form-7-to-database-extension/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/contact-form-7-to-database-extension/)
 * [Active Topics](https://wordpress.org/support/plugin/contact-form-7-to-database-extension/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/contact-form-7-to-database-extension/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/contact-form-7-to-database-extension/reviews/)

## Tags

 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [e-mail](https://wordpress.org/support/topic-tag/e-mail/)
 * [forms](https://wordpress.org/support/topic-tag/forms/)

 * 4 replies
 * 2 participants
 * Last reply from: [mattyk1972](https://wordpress.org/support/users/mattyk1972/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/creating-multiple-conditional-e-mail-forms/#post-3855279)
 * Status: not resolved