• deputy963

    (@deputy963)


    Hello and thanks for the great plugin. Setup was simple and the results were great except for one small thing.

    Note: My PHP skills don’t go much farther than spelling it correctly and I haven’t done any programming in over 25 years!

    In contact form 7 I have a select field named Station which contains various locations that are named in a manner that will be familiar to the user. Following the location is the pipe character (“|”) and the email addresses associated with the location.

    My goal it to make the entry as fool proof as possible allowing the user to select the location without knowing which email is associated with the location.

    Contact form 7 strips the data before the pipe leaving only the email address, which is used to email the form to the location, but the data added to the database is also stripped of everything but the email address.

    I could settle for the entire string being stored, but in a perfect world it would only store the data before the pipe.

    The TL;DR version:
    Contact Form 7 select options-
    “location1|email1”
    “location2|email2”
    “location3|email3”

    Contact Form 7 outputs and Database Extension stores-
    email1
    email2
    email3

    What I would like to be stored in the database-
    location1
    location2
    location3

    I hope that makes sense and this old guy would appreciate any help anyone is able, or willing, to provide!

    https://www.ads-software.com/extend/plugins/contact-form-7-to-database-extension/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Michael Simpson

    (@msimpson)

    As I looked into this, I found it is really tricky so I wrote this for you to try. To install follow the directions on this page.

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

    What I did here was change the value of the location field to the actual location, and I created another field “location_email” that contains the email. So in the CF7 definition, you can set your Email To to be [location_email] (or change it to <you field name>_email if it is not named “location”)

    Thread Starter deputy963

    (@deputy963)

    Thank you Michael. I really appreciate the help!

    Added to functions.php and php works, but causes CF7 to throw a Failed to submit error, even though data does show in the DB – without the email I might add. ??

    A sample of the actual data would be “WOOD – Dayton|[email protected]” and everything from the pipe to the right is stripped as expected. Would a simpler solution be to just grab the leftmost 4 characters as they are the important ones?

    I can sort of follow the code, but this is about all I understand.
    Line3/4 is setting variables.
    Line5 looks like it’s checking $formName against $fieldname.

    My goal is to create a new page that will populate a table based on the first 4 characters in the above field, a date field, and a null value in a third field (i.e., display WOOD, for mm/dd/yyyy, where no one has entered their initials). Add a required field for initials and a submit button to add to that database field. If I understood the first thing about PHP I think I would be as simple as a few if-else statements and echo variables in the table cells (yes, I have dreams of grandeur and little knowledge to back them up).

    Thread Starter deputy963

    (@deputy963)

    Just a little update. I created a child theme (I’ll figure this out yet)and added the code above to a new functions.php in the child and it works, but with the same error!

    Plugin Author Michael Simpson

    (@msimpson)

    Failed to send error: In the CF7 form definition, in the section where it is sending mail “To” field, what do you have there? I.e. did you follow my instructions in the last sentence of my last post? If not, CF7 is trying to send to “WOOD – Dayton” instead of “[email protected]

    Get that fixed first, then worry about chopping it down to just “WOOD”

    Thread Starter deputy963

    (@deputy963)

    Michael, I did not. In my excitement I completely forgot about changing the code in the mail section. Pretty sad, I know.

    Plugin Author Michael Simpson

    (@msimpson)

    OK. Then in the line of code where I have
    $valueSelected = $valuesArray[0];
    you could change to this to get just the first 4 chars
    $valueSelected = substr($valuesArray[0], 0, 4);
    (not sure if that is what you meant)

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to alter the information stored in the databse’ is closed to new replies.