[cfdb-save-form-post]

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.

You must be logged in to post a comment.