4

Using Contact Form 7

Situation

When a visitor fills out a form on the website, they enter:

  • Name
  • Email
  • Phone
  • Address (Street, City, State, Zip Code)
  • Message

I need to save this information to the database for future reference and I need to send an email to a central email address and to a specific email address, which changes depending upon the state of the visitor.

Basically, salespeople have territories and contacts from the website need to be sent to the main website email address as well as the person in charge of the territory.

Not all states have an assigned salesperson.

Steps Taken

I installed Contact Form DB to save all submissions to the database.

I am using the method described on the Contact Form 7 website to select the recipient - Selectable Recipient with Pipes. I am using the state name as the selectable dropdown and the salesperson's email address following the pipe.

[select* state 
"Alabama|[email protected]"
"Alaska"
"Arizona|[email protected]"
"Arkansas|[email protected]"
"California|[email protected]"
"Colorado"
]

I set up CF7's "Mail" to send to the website's central email address and to the salesperson's email address who operates in the selected state.

[email protected],[state]

The Problems

When a visitor selects a state that has an assigned salesperson, the salesperson's email address is entered into the database, not the state.

When a visitor selects a state name that does not have a designated salesperson, the state name is entered into the database, but no email is sent.

I also tried setting up "Mail" to send to [email protected] and "Mail (2)" to send to [state] (salesperson's email address), however no email was sent to [email protected] when an email address was not provided for the state name in the dropdown.

Lastly, I tried entering the state name in this format - "Alaska|". In this case, nothing was entered into the database for state and no email was sent. I tested this using both of the "Mail" options listed above.

The Solution?

Now, I understand, I can simply enter [email protected] for all state names without a designated salesperson - and that's fine.

The problem with this - the email address is still entered into the database for state.

I imagine I need a way to take the state name ([_raw_state]) and pass it into a hidden CF7 field, so it can be saved to the database, but I cannot seem to figure out how.

1 Answer 1

2

Thanks to Michael Simpson, from Contact Form DB. The solution is listed (kind of hidden) on the Contact Form DB website in the article - CF7 Menus with Pipes

Add to functions.php

function myFilter($formData) {
// Change $formData
return $formData; // be sure to return it
}

add_filter('cfdb_form_data', 'myFilter');

function location_form_handler($formData)
{
$formName = 'ExtendedContact'; // change this to your form's name
$fieldName = 'state'; // 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');

This will create an entry in the database with the state name (field before the pipe) as state and a second field (email address after the pipe) with the associated email address as state_email.

Finally, update the form definition in Contact Form 7 thus: [state_email]

NOTE

I also want to mention, in case someone else comes across this situation -

In my form, visitors who fill out the form select the state they are located in, then the salesperson who is responsible for that state receives the email. As well, I needed the email to be sent to a central (corporate) email address.

However, not all states have an assigned salesperson, being the company focuses on the mid-west.

With Contact Form 7, if the email address after the pipe was empty, emails would not be sent at all. The method outlined by Michael actually allows an empty field after the pipe.

I set up "Mail" in the form to send to the central email and "Mail (2)" to send to the salesperson (if one exists).

Not the answer you're looking for? Browse other questions tagged or ask your own question.