WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form DB
How to alter the information stored in the databse (7 posts)

  1. deputy963
    Member
    Posted 1 year ago #

    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!

    http://wordpress.org/extend/plugins/contact-form-7-to-database-extension/

  2. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    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")

  3. deputy963
    Member
    Posted 1 year ago #

    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|theemail@xxx.com" 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).

  4. deputy963
    Member
    Posted 1 year ago #

    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!

  5. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    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 "theemail@xxx.com"

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

  6. deputy963
    Member
    Posted 1 year ago #

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

  7. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    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)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic