WordPress.org

Ready to get started?Download WordPress

Forums

Contact Form DB
[Plugin: Contact Form 7 to Database Extension] Saving multiple form fields into the same column (9 posts)

  1. Moondrop
    Member
    Posted 1 year ago #

    Hello, I am using Contact Form 7, and I was wondering if it is possible to save multiple form fields into the same column. For example, is it possible to have:

    [email email-1]
    [email email-2]
    [email email-3]

    and then save them all into a column named 'Email', each entry in a new row? I already know about Changing Form Data Before it is Saved, but this saves the data into only one row.

  2. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    I suppose you could trick this out to work this way. Using the link you cited, install a filter function. In it, prepare three versions of the data structure for submission. Directly insert the first two and return the last one for the regular mechanism to insert it.

    To directly insert the first two, you basically need to:

    require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
    $cfdb = new CF7DBPlugin();
    $cfdb->saveFormData($formData1);
    $cfdb->saveFormData($formData2);
  3. Moondrop
    Member
    Posted 1 year ago #

    Thanks for the reply Michael! Unfortunately, I have not been able to get it working. I always receive only one submission entry in the database. Could you give me an example of preparing one version of the data structure, and then inserting it?

    Once again thank you for your help!

  4. Michael Simpson
    Member
    Plugin Author

    Posted 1 year ago #

    I tried the following, but it seems to hang for me on the call to $cfdb->saveFormData($formData);

    Try it and see if your luck is any better.

    functions.php

    function make_into_3_form_submissions_filter($formData)
    {
        $formName = 'three_emails'; // change this to your form's name
        if ($formData && $formName == $formData->title) {
            require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
            $cfdb = new CF7DBPlugin();
    
            $email1 = $formData->posted_data['email-1'];
            $email2 = $formData->posted_data['email-2'];
            $email3 = $formData->posted_data['email-3'];
    
            unset($formData->posted_data['email-1']);
            unset($formData->posted_data['email-2']);
            unset($formData->posted_data['email-3']);
    
            $formData->posted_data['Email'] = $email1;
            $cfdb->saveFormData($formData);
    
            $formData->posted_data['Email'] = $email2;
            $cfdb->saveFormData($formData);
    
            $formData->posted_data['Email'] = $email3;
        }
        return $formData;
    }
    
    add_filter('cfdb_form_data', 'make_into_3_form_submissions_filter');
  5. Moondrop
    Member
    Posted 1 year ago #

    Hi Michael, I am sorry for the very late reply. I tried the code you wrote, and sadly it hangs for me as well. Thank you for answering however.

  6. ch8rt
    Member
    Posted 10 months ago #

    Apologies for bringing this back but it seemed the nearest topic on the issue I'm trying to resolve – that is to post multiple entries to the same form Db from a single form.

    I don't pretend to fully understand these things, but having played around with this for a few hours it seems that the problem is that in the above example the saveFormData lines are activating this filter again, causing a continuous loop – hence the hang up.

    I've not yet decided on the implementation in my project (I'm just scoping at the moment) but adding an additional check in the 'if' statement for something that appears (or doesn't) in the first entry only seems to do the trick.

  7. Don't apologize but do start your own topic instead.

    http://wordpress.org/support/plugin/contact-form-7-to-database-extension#postform

    Adding to someone else's 6 month old topic is not the way to get support for your problem.

  8. Michael Simpson
    Member
    Plugin Author

    Posted 9 months ago #

    I think the infinite loop problem is correct.

    To make it work, I think instead you would need to do this (I haven't tried it)

    1. In the admin -> Contact Form DB -> Options list your form as one NOT to be saved.
    2. Instead of the "add_filter" call in the above code:
    - For CF7: add_action('wpcf7_before_send_mail', 'make_into_3_form_submissions_filter');
    - For FSCF: add_action('fsctf_mail_sent', 'make_into_3_form_submissions_filter');

  9. ch8rt
    Member
    Posted 7 months ago #

    I started on this issue myself and seem to have found a solution, I posted it in this thread...
    http://wordpress.org/support/topic/create-new-record-for-each-selection-in-a-submitted-form?replies=3#post-4613418

    Basically stopping the looping by checking for a field that only exists the first time around – which seems to do the trick.

    I've used this to submit multiple records to a new form database thats linked to the original by an id. eg. Mr Smith makes 4 seperate bookings.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic