Create a table to store the data in the database.

CREATE TABLE `wp_tps_forms` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `form` varchar(100) NOT NULL DEFAULT '',
  `data` text NOT NULL,
  `date` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
add_action('wpcf7_before_send_mail', 'save_form' );
 
function save_form( $wpcf7 ) {
   global $wpdb;
 
   /*
    Note: since version 3.9 Contact Form 7 has removed $wpcf7->posted_data
    and now we use an API to get the posted data.
   */
 
   $submission = WPCF7_Submission::get_instance();
 
   if ( $submission ) {
 
       $submited = array();
       $submited['title'] = $wpcf7->title();
       $submited['posted_data'] = $submission->get_posted_data();
 
    }
 
     $data = array(
   		'name'  => $submited['posted_data']['name'],
   		'email' => $submited['posted_data']['email']
   	     );
 
     $wpdb->insert( $wpdb->prefix . 'tps_forms', 
		    array( 
                          'form'  => $submited['title'], 
			   'data' => serialize( $data ),
			   'date' => date('Y-m-d H:i:s')
			)
		);
}
Contact Form 7, save submited data in the database
Tagged on:

17 thoughts on “Contact Form 7, save submited data in the database

  • April 21, 2015 at 12:24
    Permalink

    wow. great tutorial. thanks.

    Reply
  • May 11, 2015 at 11:45
    Permalink

    where this code put ????
    please tell me .

    Reply
  • January 5, 2016 at 09:17
    Permalink

    Sir how can i store the contact form id into my database, so which i can get from which form it has been submitted

    Reply
    • May 2, 2016 at 08:42
      Permalink

      Hi Bhavesh,

      Sometimes, it becomes headache to get the information about stored contact form data. But, you can solve your problem with “Save Contact Form 7” plugin.

      Save Contact Form 7 has many plus points including Search & Sort, Export CSV & PDF and many others. And, I am sure that you will find the best solution to your issue with it.

      You can find Save Contact Form 7 plugin here : https://wordpress.org/plugins/save-contact-form-7/

      Hope to hear your experience with Save Contact Form 7.

      Regards,

  • April 29, 2016 at 00:08
    Permalink

    data not working help…..

    Reply
  • September 1, 2016 at 20:20
    Permalink

    Thanks ever so much for this. I’ve been looking for a simple solution which doesn’t require a plugin to be installed, and this is it! You’re a star Lucy!

    Reply
  • September 9, 2016 at 14:48
    Permalink

    Hello, i want to create new database table while activation on contact form 7 plugin. I don’t know how can i create this table so please guide me.

    Thanks

    Reply
  • October 7, 2016 at 09:31
    Permalink

    Hello, I’m using the Save Contact Form 7 plugin, while selecting the form which we want to see the details of submitted form on drop down i’m getting “DataTables warning: table id=nimble_table_data – Ajax error.” this alert message and the submitted from details are not showing. Can anyone help me.

    Reply
  • January 11, 2017 at 07:01
    Permalink

    How to save the attachment files, I have a contact form with attachment files So can you please help me to save the attachment files.

    Reply
  • February 26, 2017 at 13:14
    Permalink

    Hi, i got a question. After created the form in CF7 with the fields, name and email. How does my new CF7 form knows which function to run? I got 2 functions in function.php.

    Reply
  • October 4, 2017 at 07:05
    Permalink

    i dnt save my contact form 7 data into database … how can i solw ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *