Saving your form in another database

Each entry submitted with Smart Forms is already stored in the wordpress database (more specifically in the table rednao_smart_forms_entry) but in case you want to store that information in a different table or database this tutorial will help you do it.

 

Requisites

We will need three things:

1.- The id of the form that you want to save, you can get this information in the main screen of Smart Forms

 

2.- The id of the fields that you want to save, this information is found in the settings panel of each field

 

3.- An empty smart forms extension (which you can create like this). In this extension we will create the code that will transfer the entry information to our database. Note: You can download the extension that i did in this tutorial so if you prefer you can use that one instead.

 

Getting Started

 

Saving the form in another database is pretty easy, in our empty extension all we need to do is this:

 

1.-  Add a hook to the ‘sf_after_saving_form’, this event is fired after the form is saved in the smart forms database. It will have a parameter called $entry which contains all the information of the submitted form.

2.- Then check the form id using $entry->FormId. If the form id is not the one that we want to save then just stop the execution of this code like this:

3.- Use $entry->GetFieldValue(fieldId) to get the value of the fields that we want to save, in this demo i only want to save the name, email and address which have the ids of rnfield4, rnfield5,rnfield6 respectively. 

4.- Save the data, this section highly depends on where and how you want to save it. For this demo i will just store it in another table (called ‘my_new_table’) which is part of my wordpress database:

And that’s it!  Now everytime a form is submitted it will also be saved in my new table =).

At the end, my code looks like this: 

Which you can Download here

Do you have any question? If so please let me know!

 

Post a Comment

Your email is kept private. Required fields are marked *