Saturday, April 7, 2012

Contact Form 7 send contact to database

UPDATE: Aug, 2016 -  There's a better solution using an add-on plugin for wordpress.



For archive purpose:

I noticed since Sept 23, 2014 (but maybe the change was made earlier since we didn't update the plugin until now) There are structure and code changes made in the new version which means this will not work. If you don't see the classes.php file you know you have the newer version (4.0 for me). New solution: modify the contact-form.php file around line 560 after:
if ( $submission->is( 'mail_sent' ) ) {
if ( $this->in_demo_mode() ) {
$result['status'] = 'demo_mode';
}
add:
require_once (getcwd().'/wsdl/func.php');
webRequest2($submission->get_posted_data());

on your own code you might need to add [0] at the end of the array fetching for each field.


Otherwise if you have an older version read the original solution:

If you use the quick form creation on wordpress with the popular Contact Form 7 while sending the data (not only to email) to a database of your CRM or related software to help manage the leads, this should help.

Although I am aware of the database extension I couldn't spend time to figure it out because we already had a database solution from a custom site that didn't use wordpress prior, so it was very simple once I found where the data is being sent:

Found the location in our install to be under /your-domain/wordpress/wp-content/plugins/contact-form-7/includes/classes.php

In this file you edit the part which is around line 278 in the submit function under the code
} elseif ( $this->mail() ) {

add your code, in my case it's two lines:
$cid=webRequest2($this->setup_posted_data());
sendAnalytics($cid);

I covered more on the analytics software here.

As simple as this you have the data and can send it to your database (which could and probably should be on different host).

Let me know what you think!

9 comments:

Anonymous said...

I have been looking for this solution for a long time. Thanks for your information. However I would like to know can we extract the form value being submitted by user so that i can do a insert statement into a external database? Contact Form 7 is not so straight forward giving the values. I am not sure how.

Now I am stuck with this.

theborisedu said...

The values are in the $this->setup_posted_data() line, this is an array which can be used to extract the values, so let's say you have a name value it will be set to $array['name'] if you set $array=$this->setup_posted_data();

Anonymous said...

Thank for the prompt reply. Not exactly sure how this work. Do you have a simple example? Let say i need 3 values, Name, Contact-No and Email-address value.

what must i do before i execute
Insert into ext_database_name (name, contact-no, email) values (?, ?, ?)

theborisedu said...

assuming you are able to connect to the external database you must have the line prior to the insert as noted in my first comment: $array=$this->setup_posted_data();

and now you are able to use the values, in your example it would look something like:

Insert into ext_database_name (name, contact-no, email) values ($array['name'], $array['contact-no'], $array['email'])

but I must warn you should sanitize values given from a users at all times, specially before an sql statement.

if you are interested in the basics there should be many tutorials online to get you started

Anonymous said...

Thanks, I got the basic idea already, will expand from there. This is really helpful and I think there must be alot of people trying to find this solution. You are great. A problem that i have for a lot time and you are kind enough to give the solution. Thank You.

theborisedu said...

welcome, thanks for visiting :)

Anonymous said...

hi EduBoris,

i was add my own code in classes.php line 279.

my code:
$email = $cf7->posted_data["your-email"];
$name = $cf7->posted_data["your-name"];
//connect to the db and post the data
mysql_connect("localhost","root","") or die('Could not connect: ' . mysql_error());
mysql_select_db("mydb") or die('Could not select db: ' . mysql_error());
mysql_query("INSERT INTO testing (name, email) VALUES('$name', '$email')") or die('Could not insert table' . mysql_error());
mysql_close();


why my code is not working at all?
thanks Boris

theborisedu said...

instead of $cf7->posted_data["your-email"];
it should be $this->posted_data["your-email"];

Ron Knee said...

Fantastic!
It's been over year since the previous comment, but there is still no tutorial as useful as this.
Thanks a lot.