BLOG

Gravity Forms: Submitting forms to 3rd Party Applications

If you develop websites in WordPress, there is a good chance that you have heard of Gravity Forms, a well-known plugin that allows you to implement forms very easily into your website.

How Does it Work?

Gravity Forms validates the data from the user, and stores it into the WordPress admin interface so you can log in and see who has submitted entries.

What is the Downside?

The only downside to Gravity Forms is that by default it does not give you an option to have a second “action,” as you have to send your forms to 3rd party software. However, with some simple coding, this can be remedied!

Adding a Second Action

Start by creating your form within Gravity Forms and adding it to a page. Once it is implemented correctly, you can start the process of adding the second action. First, we need to get the relevant ID’s of the Gravity Form fields. Open up your functions.php file (/wp-content/themes/*your-theme*/functions.php) and scroll to the very bottom, and add this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
add_action("gform_post_submission", "set_post_content", 10, 2);
 function set_post_content($entry, $form){
 //Gravity Forms has validated the data
 //Our Custom Form Submitted via PHP will go here
 // Lets get the IDs of the relevant fields and prepare an email message
 $message = print_r($entry, true);
 // In case any of our lines are larger than 70 characters, we should use wordwrap()
 $message = wordwrap($message, 70);
 // Send
 mail('travis@0to5.com', 'Getting the Gravity Form Field IDs', $message);
 }

The snippet above will hook into all forms being submitted through Gravity Forms after submission (Replace travis@0to5.com with your relevant email).  After putting this in your functions.php file, go ahead and fill out your form. Make sure to fill in ALL fields with relevant data so that we can determine which fields are associated with which IDs.

You should receive an email that looks similar to this:


Sort out the relevant IDs to the relevant fields and write them down.  For example, my name is Travis Hoglund, so the first name field = 1.3, and the last name field = 1.6, etc.

Now that we know the field ID’s, we can implement a custom curl function to submit the data through PHP, just as it is sent through a browser. Below is the complete code – it might look like a lot, but I will explain it!

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
add_action("gform_post_submission", "set_post_content", 10, 2);
 function set_post_content($entry, $form){
 // Lets get the IDs of the relevant fields and prepare an email message
 //$message = print_r($entry, true);
 // In case any of our lines are larger than 70 characters, we should use wordwrap()
 //$message = wordwrap($message, 70);
 // Send
 //mail('travis@0to5.com', 'Getting the Gravity Form Field IDs', $message);
 function post_to_url($url, $data) {
 $fields = '';
 foreach($data as $key => $value) {
 $fields .= $key . '=' . $value . '&';
 }
 rtrim($fields, '&');
 $post = curl_init();
 curl_setopt($post, CURLOPT_URL, $url);
 curl_setopt($post, CURLOPT_POST, count($data));
 curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
 curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
 $result = curl_exec($post);
 curl_close($post);
 }
 if($form["id"] == 8){//Join Our Mailing List
 $data = array(
 "first_name" =>     $entry["1.3"],
 "last_name" =>     $entry["1.6"],
 "title" =>         $entry["2"],
 "company" =>     $entry["3"],
 "street" =>         $entry["4.1"],
 "city" =>         $entry["4.3"],
 "state" =>         $entry["4.4"],
 "zip" =>         $entry["4.5"],
 "country" =>     $entry["4.6"],
 "website" =>     $entry["10"],
 "email" =>         $entry["5"],
 "phone" =>         $entry["6"],
 "industry" =>     $entry["7"],
 "description" => $entry["8"],
 "formName" => "join-mailing-list-98368402345"
 );
 post_to_url("https://thirdPartyApplication.php", $data);
 }
 }

As you can see, I commented out the email code because we do not need this anymore.  If you have access to the third party forms, you will want to use software like Firebug (or you can just view source) and get the name attribute of each field.

1
2
<form action="https://thirdPartyApplication.php" method="post">
 <input style="" value="" id="field1">

I used Firebug to grab this snippet from my third party form. You will notice that the form is submitting to https://thirdPartyApplication.php so that is the value that must go in your code. Below that is the third party code for the First Name Field which has a name of first_name, so that is what it MUST be in the above PHP code if you want the third party to grab your POST variables correctly.

Most forms use hidden fields to determine what form is being submitted, so make sure you include them in your PHP code as well.  Here, the name and value are important:

1
<input type="hidden" value="join-mailing-list-98368402345">

As long as you make sure to include all of your form fields, use the correct IDs, and submit them with the relevant name attributes from the third party forms, you get the best of both worlds. Gravity Forms will validate your forms, save them to the admin interface, and your third party software will also receive them.

Happy Gravity Forms!

Travis Hoglund
Zer0 to 5ive Senior Developer