[cfdb-save-form-post]

October 18th, 2014

AVAILABLE AS OF RELEASE  2.1

WARNING: Only use this short code to capture data from a form that is NOT created by Contact Form 7 (CF7), Fast Secure Contact Form (FSCF) or JetPack form. If you are using one of those, then this plugin will automatically capture the form submissions. This short code is for special cases.

This plugin was written to capture data from Contact Form 7 (CF7),  Fast Secure Contact Form (FSCF) and JetPack forms but what if you don’t want to use them? What if you want to put an old-fashion HTML FORM tag in your post or page and have it submit to a post or page where the data will be put in the database? You may want to do this if you are trying to do some fancy things on the form’s target page that you just can’t do with CF7 or FSCF.

In that case you can use this shortcode on the page that a form posts to to make it save the submission data to the database. This short code does not generate any output on the page where it is placed.

How to do this:

First: Create a form on a page or post. Example

<form action="" method="post" enctype="multipart/form-data">
   <input type="hidden" name="form_title" value="People"/>
   First Name: <input type="text" name="fname" value=""/><br/>
   Last Name: <input type="text" name="lname" value=""/><br/>
   <input type="file" name="upload"/> <br/>
   <input type="submit" />
</form>
  1.  In this example, action=”” making the form post to the same page. You can make it the same or a different page as you like.
  2. You must use method=”post” and not method=”get”
  3. Give the form a hidden field named “form_title” . This will be used to give the form a name in the database. If not given, the name ‘Untitled’ is used.
  4. Do NOT name any input fields as “name” like this: <input type=”text” name=”name”/> because WordPress uses “name” and you will navigate to a WordPress error page.

Second, somewhere on the page where this form posts (action page) simply put the short code:

[cfdb-save-form-post]
  1. The short code will save the data to the database
  2. No page output is generated (but you may get an extra new line if you have the short code on its own line, WordPress can inject a new line)
Warning: No spam protection!
This method does not have any spam protection (like CF7 and FSCF do). Be ware that anyone that can access the action page can post any data. They can spam your database.
  1. Annika
    May 12th, 2012 at 10:18 | #1

    Hi, this plug in is great! However, I am afraid I will have to learn php anyhow…
    I have a FSCF form. When i submit the form to the db I also wan to calculate the total (add all the answers) and store the sum in the db.
    I have bought the extension and created a new column in the db. What is the easiest way to store the sum?
    Thanks!

    • Michael Simpson
      May 12th, 2012 at 13:31 | #2

      Storing the sum is difficult because the plugin doesn’t support automatically adding additional fields to a form submission. However, it is a lot easier to create a custom short code that would display form data and add the sum. In that case the sum is not stored in the DB, but is generated each time the short code executes to display data on a web page that you create. A similar and much easier solution is to do an Excel IQuery export. In this case, you could set up an Excel spreadsheet that automatically updates with the latest data, and add your own column doing whatever summation you like. Consider whether these kinds of approaches would be adequate for your needs.

  2. Tim Helmer
    November 10th, 2012 at 01:19 | #3

    [cfdb-save-post-post] ? (quote below “second” as above)
    didn’t you mean
    [cfdb-save-form-post]

    BTW thanks for the great plugin..

  3. January 3rd, 2013 at 15:47 | #4

    I have [cfdb-save-form-post] on the same page as the form. However, when the form is submitted, I get two entries into the database. Any thoughts?

  4. Jason
    February 3rd, 2013 at 16:10 | #5

    I have the plugin installed, but the form is within a custom template that does not include most of the wordpress headers/footers. So is there a way to add this functionality without a shortcode? direct php?

    thanks!

  5. Michael Simpson
    February 5th, 2013 at 21:19 | #6

    @Jason
    On the page that receives the form post try this:

    require_once(ABSPATH . ‘wp-content/plugins/contact-form-7-to-database-extension/CFDBShortCodeSavePostData.php’);
    $saveForm = new CFDBShortCodeSavePostData();
    $saveFom->handleShortcode(null);

  6. February 8th, 2013 at 13:44 | #7

    I do not get any records written to the database. I intend to use this facility to capture jetpack subscription info.
    For testing purposes, I have set up a (private) post containing the following. It appears as if it is working, but nothing is written to the database.

    UNFORTUNATELY MY HTML HAS BEEN SWALLOWED !! I will need to resubmit when I find out how to escape the html
    message:
    [cfdb-save-form-post]

    • Michael Simpson
      February 8th, 2013 at 14:04 | #8

      This was not designed to work with JetPack subscriptions. I don’t if you are trying to capture the event where a user subscribes or what. And I don’t know if such an event results in a form post to a content page (which could conceivably be captured but I’d be surprised if it worked that way) or some over interaction with the server that cannot be captured.

      I do not understand what you mean by “swallowed” html. If you put a short code on a page, you might need to first switch to the post editor to “Text” instead of “Visual” to put in html and short codes so they are not escaped.

  7. February 8th, 2013 at 17:44 | #9

    Michael – thank you for a quick response.

    I am trying to capture the event whereby a user uses a jetpack widget to subscribe in order to follow a blog. I couldnt get this to work using your plugin/shortcode, so I tried using different html (unrelated to jetpack) to ascertain whether the fault lay with the way I was trying to use your plugin/shortcode. I tried to include this html in my earlier post but the html was swallowed by the code which makes this forum work. I think this is because I did not use backticks. I have tried again to include this html as below (but this time with backticks).

    Assuming the html below does display itself in the , can you please guide me as to why it does not result in any data being added to the database.
    `

    message:

    [cfdb-save-form-post]
    `

  8. February 8th, 2013 at 17:47 | #10

    Backticks did not work, so I have tried replacing left and right chevrons with asterisks

    *form action=”” method=”post” enctype=”multipart/form-data”*
    *input type=”hidden” name=”form_title” value=”People”/*
    message: *input type=”text” name=”message” value=””/* *br/*
    *input type=”submit” /*
    */form*

    [cfdb-save-form-post]

    [cfdb-save-form-post]>

    [cfdb-save-form-post]

  9. Aaron
    February 15th, 2013 at 08:04 | #11

    I am trying to save custom form data with dynamically added rows. The form initally has one row (with 5 text inputs) but users can add rows (using jQuery) as needed. I can get this to work by incrementing the input names (e.g. inputa1, inputb1, … inpute1 –> inputa2, inputb2, … inpute2) however, as expected, this adds additional columns to the datatable in the backend. I would like for each additional row to save the data under the same column heading as the first row. How could this be accomplished? By the way, thanks for the great plugin!

    • Michael Simpson
      February 15th, 2013 at 09:04 | #12

      When a form is submitted it comes as a set of field-name/field-value pairs. The plugin just dumps those in the DB as column name & values. So you should have your JavaScript name new dynamic fields to be the column names you want. If you want to change the column names on the back end, refer to the page on this site about changing data before it is saved. link

  10. Aaron
    February 15th, 2013 at 09:08 | #13

    @Michael Simpson
    But if I name the newly added field with the same name as the input on the first row, the plugin only saves the last input with that name. For example, if I have Row1 and Row 2 , then only the Row 2 input value gets saved.

  11. Aaron
    February 15th, 2013 at 09:10 | #14

    @Aaron
    Sorry. some of my comment was modified because I put pseudocode in…
    But if I name the newly added field with the same name as the input on the first row, the plugin only saves the last input with that name. For example, if I have Row1 with input named “field1″ and Row 2 with input named “field1″, then only the Row 2 input value gets saved.

  12. Michael Simpson
    February 15th, 2013 at 09:53 | #15

    @Aaron
    The browser can only submit one value for each field name. So if you have two fields in your form both named “field1″, then the browser will pick one and submit that. Seems like your browser picks the last one. This is a limitation of HTTP, not the browser or the plugin.

  13. Aaron
    February 15th, 2013 at 10:21 | #16

    @Michael Simpson
    Right. I understand the limitation. I was hoping there was a way to get around this using the plugin. Is there a way to have the plugin loop through the fields and add them?

  14. Michael Simpson
    February 15th, 2013 at 14:44 | #17

    No b/c the plugin only receives one value for field1 from the browser.

  15. Aaron
    February 15th, 2013 at 16:41 | #18

    @Michael Simpson
    Thanks for your quick responses. I was finally able to get this to work by having the users dynamically add new forms, rather than rows, and submitting those forms in a loop via Ajax. I’m not sure if it is the best way to do things, but it works.

  16. Gerald Bird
    May 10th, 2013 at 13:12 | #19

    Just a note that your instructions on this page have one mistake in them. Part of the way down it says the shortcode is [cfdb-save-post-post]. Which obviously should be [cfdb-save-form-post] like the title. I was hyper-focused enough to copy and paste the wrong shortcode and wonder why it didn’t work. So someone else might be. lol.

    Thanks for the plug-in it works great, as advertised. I am using it with Visual Form Builder.

    • Michael Simpson
      May 10th, 2013 at 13:14 | #20

      Thanks, will fix.

  17. pako69
    July 8th, 2014 at 12:58 | #21

    Hello
    First, please excuse for my bad english: I’m french.
    I was using CF7 but that was before I now use an new WordPress theme: Enfold that came with it’s own forms.
    So i tried the [cfdb-save-form-post] in a page where I have a Enfold form ans yes it’s works but when I look at the data, some characters are not displayed nicely, eg:
    a email adresse look like this: name%40mydomain.com
    Spaces are: %20
    A [ is: %5B
    A ] is: %5B
    and so on…

    thank you

    • Michael Simpson
      July 8th, 2014 at 16:32 | #22

      Those encodings (such as %20 for space) are how special characters are encoded in URLs. I supposed that your form plugin is encoding the information submitted with the form but not decoding it. CFDB is then picking up the encoded values.

      You could add a filter to try to correct the data. I think this code will fix it:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      
      function fix_urlencoded_form_data($formData) {
          $formName = 'YOUR-FORM-NAME'; // change this to your form's name
          if ($formData && $formName == $formData->title) {
              foreach ($formData->posted_data as $name => $value) {
                  $formData->posted_data[$name] = urldecode($value);
              }
          }
          return $formData;
      }
       
      add_filter('cfdb_form_data', 'fix_urlencoded_form_data');
  18. pako69
    July 10th, 2014 at 02:13 | #23

    Hello Michael

    Thank you fot this code but it do not work, nervermind I have modified the enconding of my theme post ajax form.

    So the shortcode [cfdb-save-form-post] works very well inside a page but I have many Forms that are not specially in a page.
    Is there a way to use this shortcode on the whole site? That way it will trigger any forms tat are posts

    Thanks a lot.

  19. Michael Simpson
    July 10th, 2014 at 10:51 | #24

    @pako69
    I am not aware of a way to catch all form submissions.

  20. lw2014
    July 18th, 2014 at 00:19 | #25

    I’m trying to use this plugin on an html form that uses an ajax function to post the data.
    So I have my form, and I’ve added the hidden field.
    Something as shown below:


    Other Input Fields

    Submit

    In the code I’m adding an action to the function that processes the form. I’m also sending an email.

    add_action(‘wp_ajax_cg_make_appt’, ‘cg_make_appt’);

    And then I redirect the page to a thank you page.

    I tried adding [cfdb-save-form-post] to both the form and the thank you page, and on both cases I can see the [cfdb-save-form-post] text in the website and no data is saved in the database.

    Where should the [cfdb-save-form-post] code be inserted in this case?

    Thanks!

    • Michael Simpson
      July 18th, 2014 at 07:15 | #26

      @lw2014
      Using the short code will not work for an AJAX submission. Instead, in your AJAX handler function (cg_make_appt) try calling the code that handles the short code. Try adding this:

      1
      2
      3
      4
      5
      
      function cg_make_appt () {
        require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBShortCodeSavePostData.php');
        $handler = new CFDBShortCodeSavePostData;
        $handler->handleShortcode(null);
      }
      • Michael Simpson
        July 18th, 2014 at 07:24 | #27

        …Actually there is a simpler way now that I think about it:

        1
        2
        3
        
        function cg_make_appt () {
          do_shortcode('[cfdb-save-form-post]');
        }
  21. lw2014
    July 18th, 2014 at 11:01 | #28

    @Michael Simpson

    I tried both options and none of them worked. No records where added to the database.
    I dont have any form plugins so my Contact DB Settings are all set to false on the “Capture form submissions from …”
    The form I’m trying to retrieve data from was already created. Is there something different that I need to setup to get this form to retrieve the data?
    Thanks!

  22. Michael Simpson
    July 18th, 2014 at 13:41 | #29

    @lw2014
    Those setting are irrelevant to your situation.

    One possibility: the AJAX submission should work if it is doing a POST, but won’t if it is going a GET.

  23. lw2014
    July 21st, 2014 at 10:38 | #30

    @Michael Simpson

    So, I finally found the issue but I’m still trying to get the data into the DB.

    The ajax posted data comes as:

    Array ( [action] => cg_make_appt [cg_data] => form_title=DBAppointments&location=1&cg_date=07%2F21%2F2014&cg_time=9%3A00+AM&selectaservice=Air+Conditioning&year=1111&make=Test&model=Test&comments=Test+data+to+DB&fname=Test+name&email=test%40test.com&phone=214-555-5555&cg_captcha_entry=ZrlBKC )

    So, the form data is actually under $_POST['cg_data']

    I created a new method handleShortcode_TEST (a copy from the one you provide in the plugin), and parsed the data the way I needed.

    I then try to submit the data to be inserted in the DB as follows: do_action_ref_array(‘cfdb_submit’, array(&$data));

    where $data has:

    ( [title] => DBAppointments [posted_data] => Array ( [location] => 4 [cg_date] => 07/23/2014 [cg_time] => 12:30 PM [selectaservice] => Clutches [year] => 1984 [make] => R [model] => 4 [comments] => TEST COMMENTS [fname] => test name [email] => test@test.com [phone] => 214-492-9033 [cg_captcha_entry] => WKeNMM ) [uploaded_files] => Array ( ) )

    But still no records are added to the database.

    Any ideas why?

    Thanks!

  24. Michael Simpson
    July 21st, 2014 at 11:37 | #31

    @lw2014
    Maybe there is an error in your code? Post the code from your handleShortcode_TEST() method.

  25. lw2014
    July 21st, 2014 at 12:18 | #32

    @Michael Simpson

    The function is as follows:
    public function handleShortcode_TEST($atts, $content = null) {

    if (is_array($_POST) && !empty($_POST)) {

    $fdata = explode(‘&’, $_POST['cg_data']);

    wp_mail(array(‘XXX@gmail.com’), ‘POST DATA RECEIVED’, print_r($fdata,true), “From: test “);

    foreach($fdata as $fdata_item) {
    $fdata_array = explode(‘=’, $fdata_item);
    $cg_data[urldecode($fdata_array[0])] = urldecode($fdata_array[1]);
    }

    $posted_data = array();

    $uploaded_files = array();

    // Get posted values

    foreach ($cg_data as $key => $val) {

    if ($key != self::FORM_TITLE_FIELD) {

    $posted_data[$key] = $val;

    }
    else{
    $title = $val;
    }

    }

    wp_mail(array(‘XXX@gmail.com’), ‘AFTER DATA PARSING – Title:’ . $title , print_r($posted_data,true), “From: test “);

    // Prepare data structure for call to hook

    $data = (object)array(‘title’ => $title,

    ‘posted_data’ => $posted_data,

    ‘uploaded_files’ => $uploaded_files);

    wp_mail(array(‘XXX@gmail.com’), ‘BEFORE DATA INSERT’, print_r($data,true), “From: test “);

    // Call hook to submit data

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

    }

    }

    I’m receiving all emails with the following data:

    First Email:

    Array ( [0] => form_title=DBAppointments [1] => location=4 [2] => cg_date=07%2F23%2F2014 [3] => cg_time=12%3A30+PM [4] => selectaservice=Clutches [5] => year=1984 [6] => make=R [7] => model=4 [8] => comments=TEST+COMMENTS [9] => fname=test+name [10] => email=test%40test.com [11] => phone=214-555-5555 [12] => cg_captcha_entry=WKeNMM )

    Second Email:

    Array ( [location] => 4 [cg_date] => 07/23/2014 [cg_time] => 12:30 PM [selectaservice] => Clutches [year] => 1984 [make] => R [model] => 4 [comments] => TEST COMMENTS [fname] => test name [email] => test@test.com [phone] => 214-555-5555 [cg_captcha_entry] => WKeNMM )

    Third Email:

    stdClass Object ( [title] => DBAppointments [posted_data] => Array ( [location] => 4 [cg_date] => 07/23/2014 [cg_time] => 12:30 PM [selectaservice] => Clutches [year] => 1984 [make] => R [model] => 4 [comments] => TEST COMMENTS [fname] => test name [email] => test@test.com [phone] => 214-492-9033 [cg_captcha_entry] => WKeNMM ) [uploaded_files] => Array ( ) )

    I’m calling the method as follows:

    require_once(ABSPATH . ‘wp-content/plugins/contact-form-7-to-database-extension/CFDBShortCodeSavePostData.php’);
    $handler = new CFDBShortCodeSavePostData();
    $handler->handleShortcode_TEST(null);

  26. Michael Simpson
    July 21st, 2014 at 12:27 | #33

    @lw2014
    In your method, variable $cf_data is not defined. I think it is supposed to be the $fdata in your case.
    And I would set $title to a default value right before the “foreach” in case it doesn’t get set in the “else” clause.

  27. lw2014
    July 21st, 2014 at 13:13 | #34

    @Michael Simpson
    Thank you Michael. That was not causing any issues.
    The problem was that the plugin was activated on the wrong site (using a network site).
    I now have seen a record :)

Comments are closed.  Go To Support Forum