Submitting Data via PHP

August 26th, 2015

Perhaps you wish to save data from a form of your own, not CF7 or FSCF. Or perhaps you just want to programatically push data into the DB. To do this there are two basic things you will need to know:

  1. How to struture your form data so that the plugin knows how to consume it
  2. How to call the plugin’s save data function

 

Structuring the data

Data should be structured like this:

$data = (object)  array(
    'title' => 'form-name',
    'posted_data' => array(
    'fname' => $_POST['fname'],
    'lname' => $_POST['lname'],
    'email' => $_POST['email']),
    'uploaded_files' => null);

Where ‘form-name’ is the name of the form, and ‘fname’, ‘lname’, and ’email’ are form fields in this example. Replace them with the fields from your form.

Uploaded files

‘uploaded-files’ (if used) is an associative array of field_name => pathnames on the local drive to files that are uploaded. For example:

uploaded_files => array ( 'field-name' => '/path/to/the/filename' )

When there is a file upload, the information about it appears twice.

  • First, it appears in the list of fields (where you see fname, lname, etc. above) as ‘field-name’ => ‘upload-filename’
  • Second it appears inside the uploaded_files associative array as ‘field-name’ => ‘path-to-file’ (use the same ‘field-name’ in both).

Files uploaded from a form post get saved to a temporary disk location. You can find this using the PHP $_FILES variable. Here is an example:

$data = (object)  array(
    'title' => 'form-name',
    'posted_data' => array(
    'upload1' => $_FILES['upload1']['name'],
    'email' => $_POST['email']),
    'uploaded_files' => array ('fileupload' => $_FILES["upload1"]["tmp_name"] ));

 

Delete When Done

When done processing an uploaded file then delete (unlink) the files when done.

 

Calling the Plugin

 

Directly use the Plugin

require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php');
$plugin = new CF7DBPlugin();
$plugin->saveFormData(&$data);

 

Or Use a hook

Both CF7 and FSCF send form submission data to hooks that this plugin subscribes to. You can simply publish your form submission to one of those hooks. The advantage is that you do not need to include the CF7DBPlugin.php file, it is a de-coupled approach. The disadvantage is that any other plugin listening to the hook will also get the data (but maybe you want that…)

You can use either hook. You don’t need to have these other plugin’s installed. A hook is just a name.

Use CF7’s Hook

do_action_ref_array( 'wpcf7_before_send_mail', array( &$data) );

Or Use FSCF’s Hook

To Do: We need to have CFDBPlugin have its own hook to listen to.

do_action_ref_array( 'fsctf_mail_sent', array( &$data ) );

To Do:

On writing this I realize that the plugin should have its own hook so that people can use that decoupled approach without needing to worry about other hook subscribers. As of writing the current version is 1.8.3. I will put the following hook in version 1.8.4.

do_action_ref_array( 'cfdb_submit', array( &$data ) );

 

  1. Noob
    August 20th, 2011 at 06:25 | #1

    Hi everyone,

    first of all i apologize for my english…

    so i have a problem which starting to make me crazy!!
    i am trying to do this :
    using this plugin with cf7, everything is working great BUT i would like to programatically push data into the DB. I know that everything is explained in this section , but i don’t want to write my own code to insert datas in another table ( not cf7dbplugin_submits )
    I don’t know where to write that , in which method..
    i would like actually in the same method who inserting datas in the cf7dbplugin_submitstable inserting too in another table!!

    thanks a lot
    have a nice week end

  2. Noob
    August 20th, 2011 at 08:29 | #2

    so actually i found the action it’s action=”/me/category/acceuil/#wpcf7-f4-p101-o1″
    but i have no idea what is the meaning of wpcf7-f4-p101-o1;
    i think if i found that, i could write the code no?
    thanks again

  3. Michael Simpson
    August 20th, 2011 at 13:26 | #3

    @Noob
    Try subclassing CF7DBPlugin and overriding the getSubmitsTableName() method
    require_once(ABSPATH . ‘wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php’);

    class MyClass extends CF7DBPlugin {
    public function getSubmitsTableName() {
    return ‘my_table_name’;
    }
    }

    $plugin = new MyClass();
    $plugin->saveFormData(&$data);

  4. Michael Simpson
    August 20th, 2011 at 13:28 | #4

    @Noob
    I don’t know what ‘wpcf7-f4-p101-o1’ is. That must be related to the CF7 plugin, not this plugin.

  5. Noob
    August 20th, 2011 at 23:14 | #5

    thanks a lot michael i am gonna try that .
    my problem is that i don’t know where to instantiate that class, i read all the code about the pluginlifecycle and i am not sur to understand everything.

    Because when the forms is sending i believe that first the mail is sent ( from de CF7 plugin then your plugin is used to persists the datas to the table.

    but i don’t understand when .

    i tryed to write a code into the method “saveFormData($cf7)” for insert the datas to my own table ( this table has not the same structure of cf7dbplugin_submits table, my client has a simple table who is matching with the fields of the forms) but it doesn’t work.

    do you know why?

    anyway thanks a lot for your answer i ll try you advice.

  6. Noob
    August 22nd, 2011 at 08:35 | #6

    sorry but still have problems!! 🙂
    the CFDB7Plugin is instanciate in the function CF7DBPlugin_init($file) but who calls that function?
    my only probleme is that i don’t know when things are done.
    i don’t understand the process , for example in your plugin where the methode ->saveFormData is called? .
    I guess is the CF7 plugin who retrieve all data’s from the FORM and then sends the data to yourplugins or something but where this connection is done?
    sorry for all the questions michael 🙂

  7. Michael Simpson
    August 22nd, 2011 at 11:06 | #7

    @Noob
    There are several different approaches you can use. I many not quite understand how you want to use this.

    1. If you just want to change the behavior of the existing plugin in place, then the easiest thing is to edit the code and change the getSubmitsTableName() method. But every time you update, you need to re-apply your change.

    2. If you are writing a custom PHP page template and it receives data posted from a form, (not using CF7 or FSCF) then you can use code like it is described in this page.

    3. Similar to (2), you could put that code in a shortcode and put that shortcode on a page instead of writing a PHP file for a custom page.

    4. Write you own plugin (not as hard as it sounds if you can code a little PHP) that does the same thing as this plugin (without all the bells & whistles). Subscribe to the hook from CF7 or FSCF and write the data to the table you want, how you want it.
    How that works: CF7 (and also FSCF) both have hooks that they pass the form data to when it is submitted. This plugin subscribes to those hooks, gets the data and saves it to the DB. Specifically, for CF7, there is a line in CF7DBPlugin.php
    add_action(‘wpcf7_before_send_mail’, array(&$this, ‘saveFormData’));
    That means when CF7 publishes data to its ‘wpcf7_before_send_mail’ hook, it calls CF7DBPlugin.saveFormData($formData). Same thing for FSCF, but it publishes data to a hook called ‘fsctf_mail_sent’. Find more information here: http://wordpress.org/support/topic/plugin-contact-form-7-insert-contact-name-and-email-in-database-after-successful-submission?replies=21

  8. Noob
    August 23rd, 2011 at 13:26 | #8

    thanks a LOT michael i wanted exactly to do the number 4 scenario…. all the solution came from the line :
    add_action(‘wpcf7_before_send_mail’, array(&$this, ‘saveFormData’));
    so i hooked the action that i wanted….

    PERFECT 🙂

  9. frankie
    December 5th, 2011 at 06:11 | #9

    Hi,
    was looking for a plugin to be able to insert form data into the database but having read many pages on this site . . . . I’m still struggling to grasp even the basics of how to achieve this! I’m not very experienced with php, and was wondering if it was possible to have an example page of code for a standard form, then it would point me in the right direction to be able to create my own form and match form fileds to database fields etc.
    Thanks

    • Michael Simpson
      December 5th, 2011 at 10:32 | #10

      If by “insert form data into the database” you mean capture a form submission into the database, then all you have to do is install and activate the plugin and it will capture Contact Form 7 and Fast Secure Contact Form submissions. If you are creating your own form in HTML however, then you need to put the [cfdb-save-form-post] short code on the page that receives the form submission. The data goes into a table managed by the plugin, not a table of your choice.

      If instead by “insert form data into the database”, you mean to import data from some other source into the DB, that is a more detailed discussion.

  10. Cole
    February 7th, 2012 at 14:01 | #11

    Is it possible to update existing data in the database via PHP? I know there is a form editor within the plugin, but the form being submitted is extensive and the editor interface is not cutting it usability wise. I have created a plugin that will allow you to view submitted data in the exact form/layout they submitted from. I just can’t figure out how to resubmit that data and over write the data for that form entre.

    Any thoughts? 🙂
    Thanks!

    • Michael Simpson
      February 7th, 2012 at 17:33 | #12

      In the database that the plugin uses, you can uniquely identify a form field by submit_time, field_name. So if you display something on a web page to edit it, your backend handing the post would have to create a SQL call like update wp_cf7dbplugin_submits set field_value = ‘‘ where submit_time = and field_name =

  11. March 12th, 2012 at 16:33 | #13

    It’s hard to determine if this plugin supports a silent send to a 3rd party database such as infusionsoft. Can anyone help me by letting me know if there is a simple plugin to support the infusionsoft 3rd party database?

    Thank you,

    • Michael Simpson
      March 12th, 2012 at 17:07 | #14

      This plugin does not.

  12. Kate
    April 1st, 2012 at 02:18 | #15

    If I want to store my customers’ data from my shopping cart’s payment form, what’s the easiest way to pull and store the data?

    If I use the hook method, how can I give it a form name so it will go to the same data table as my CF7’s data table?

    Thank you!

  13. Michael Simpson
    April 1st, 2012 at 19:36 | #16

    @Kate
    (1) Without knowing anything about your shopping cart form, it is hard to say. But if the form submits to a page you control, then you can add code like the above to your page template to push the data into the DB. But if it submits to some WP ajax URL, then you could add your own action to that ajax hook to (add_action) catch the data and put the code from this page in that function. But I think you would have to write a really simple plugin to get that add_action in there.

    (2) Set the form name in the input data structure ‘title’ => ‘form-name’ The form submission does not give you the form name but typically the form submits to a specific page where you call the hook (and is the only form to do so), so you can just hard-code the form name in the data structure you are passing to the hook.

  14. Marco
    May 9th, 2012 at 18:31 | #17

    Hi, Thanks for the great plugin, here is something I am encountering:

    $data = (object) array(
    ‘title’ => ‘Grant Application’,
    ‘posted_data’ => array(
    ‘first-name’ => $_POST[‘first-name’],
    ‘your-email’ => $_POST[‘your-email’]));

    do_action_ref_array( ‘cfdb_submit’, array( &$data ) );

    is giving me:

    Warning: Invalid argument supplied for foreach() in /home/curearth/public_html/wp-content/plugins/contact-form-7-to-database-extension/CF7DBPlugin.php on line 479

    data gets into the db/table…

    Any advise? Thank you

  15. Marco
    May 10th, 2012 at 21:31 | #18

    My bad coding and spelling…I figured this out. ^^^

  16. Michael Simpson
    May 11th, 2012 at 22:32 | #19

    @Marco
    Try:

    $data = (object) array(
    ‘title’ => ‘Grant Application’,
    ‘posted_data’ => array(
    ‘first-name’ => $_POST[‘first-name’],
    ‘your-email’ => $_POST[‘your-email’]),
    ‘uploaded_files’ => array());

  17. Brian
    June 12th, 2012 at 13:02 | #20

    Question, we need a way for visitors to post reviews about specific posts then retrieve those reviews to display on that post’s page. So instead of general info being submitted by the form and stored in the database we need it to be about the topic they are viewing only. Basically we’re creating a fishing report site so each fishing spot would have its own post (using custom post types). If they fish at XYZ Lake then we want the form to identify with the XYZ Lake post_id so we can summon all ‘reviews’ from the database for that id and display them on the page using the shortcodes. So, 1) does it do this? and, 2) can it do it?

    Fingers crossed, we’ve been looking all over for something that would provide this kind of functionality. Thanks in advance!

    • Michael Simpson
      June 12th, 2012 at 15:36 | #21

      If your form that is put on a certain post includes a hidden field that identifies that post (like a post id or category) then that value also gets saved in the DB associated with those submissions. Then a short code can be used on that page with a filter constraint to look for form submissions with that id.

  18. August 21st, 2012 at 15:08 | #22

    Hallo Mr. Simpson,

    Thanks for create this great plugin.

    Since i’m not expert in php, could you please fix my custom code (i have a theme custom template) to store a upload file to server.
    I already try times an times but unfortunately its doesnt works.

    here is my themes template file . http://pastebin.com/5eBeUKKn

    I really Appreciate your kindness to help me.

    Regards,

    David

  19. Tyatt Leung
    January 26th, 2013 at 11:35 | #23

    Hi thanks for the amazing plugin! It was working really well with CF7.
    However I was trying to use your instructions above to submit to the DB directly from a php file, and got the following error:

    PHP Fatal error: Call to undefined function get_option() in /hermes/bosoraweb056/b554/nf.northyorkmusicfestival/public_html/nymf/wp-content/plugins/contact-form-7-to-database-extension/CF7DBOptionsManager.php on line 135

    I searched highs and lows but couldn’t figure out this problem. Have anyone encountered this problem before?
    Thanks in advance!

  20. Michael Simpson
    January 26th, 2013 at 12:13 | #24

    @Tyatt Leung
    That error means that the WordPress function “get_option” has not been PHP “included”. The plugin files don’t work in isolation, they only work inside of WP’s context so it has access to its functions and variables.

    This may be a matter of how you are trying to run the script. I haven’t tried this in a long time, but I think if you put your PHP file on your server and point your browser at it to run it, it should pick up the WP environment and run.

    Otherwise you need to figure out what “include” directives to add to your PHP file if you are running it on the command line.

Comments are closed.  Go To Support Forum