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

Post a Comment

(13) Responses to “Gravity Forms: Submitting forms to 3rd Party Applications”

  1. Deborah says:

    I’m trying this to post form data to Leads 360. We provided them with our field ids and they mapped the data on their end, but I don’t know their corresponding name attributes. Can this code be changed to post over our ids without mapping them to their name attributes?

  2. Deborah says:

    Is there a way to test or see what is being sent in the posting string?

  3. Travis brilliant help – but could I just clarify the final parts of the code

    $data = array(
    “first_name” => $entry[“1.3”],
    “last_name” => $entry[“1.6”],
    “title” => $entry[“2”],

    is the “first_name” the actual value / text as per in the 3rd party application ?

    My 3rd party application field for First name is something like MK-17

    so what would my code be ?

    “MK-17” => $entry[“1.3”],

  4. Vinny says:

    Hi Travis, this tutorial is great and has helped with our situation. However we have conditional logic within our form, how can we post the fields when they are variable? Not sure if I am asking the correct way, but how bout I point you to our site http://www.protomojo4.com.au/submit-resume

    Please take a look at our form and let me know your thoughts.

    Cheers

    Vinny

  5. Jihan says:

    Is is actually working for u? I need help with very urgently. I need to submit some fields values of gravity form with woocommerce. I have talked to many developers but they are failed to do. plz help me out !

  6. Pranab says:

    How can I do this to only certain Gravity forms?

  7. Pranab says:

    I would also like to thank you for this. I am trying to trigger the paypal API on form submit.

  8. John says:

    Thank you for this tutorial! It looks like the hook used here has been depreciated, here is some more info on using this method in the Gravity Form Documentation:
    http://www.gravityhelp.com/documentation/page/Gform_after_submission

  9. Daniel K says:

    Travis, I tried doing this and it caused the following error which has shut down the site entirely:

    Parse error: syntax error, unexpected $end in /home/content/68/10856168/html/wp-content/themes/pagelines/functions.php on line 36

    Any recommendations?
    Please help.

  10. Taryn Reuter says:

    Deborah, Did you get this to work with leads360? I too am having a tough time with getting gravity forms to post to them. Please let me know if you did. thanks

  11. Just awesome. Im experimenting on a WP based data handling and this just solved my problem.

    The perfect data validation + functionality package.

  12. Michael Ianni says:

    Example: This works with the Genesis v.1.9.2 Child Theme News version 2.1 and WordPress 3.5.1. And HTTPAPI. This code example works with 1 Gravity Form and would need to be expanded to incorporate other forms.
    —–
    <?php
    //Place this code at the bottom of functions.php in your child themes wordpress folder
    // Add a second Gravity forms action to 3rd party api
    add_action("gform_post_submission", "set_post_content", 10, 2);
    function set_post_content($entry, $form){ //Gravity forms validated the data

    //Use to send mail containing form id's for testing only comment out when not in use
    // 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('support@kaleidico.com', 'Getting the Gravity Form Field IDs', $message);

    //Curl 3rd party call post to api
    if ($form["id"] == 4) {
    $url = "http://api.domain.com/bin/api_web&quot;; //1. URL to use for HTTPAPI (required)
    $ch = curl_init();
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    // set the target url
    curl_setopt($ch, CURLOPT_URL, $url);

    // howmany parameter to post
    curl_setopt($ch, CURLOPT_POST, 7); //2. number of fields under parameters

    // set parameters
    $cmd = "&c=sub"; //subscribe command
    $myrealm = "&r=somerealm"; //realm (required)
    $mypass = "&p=somepass"; //api password (required)
    $mylist = "&list_id=5"; //the list being subscribed to
    $myformat = "&format=99"; // using multipart mime included html
    $mytestoption = "&force_sub=1"; //adds even if previously deleted – globally opted out, REMOVE AFTER TESTING!!

    $email = $entry["2"];
    $name = $entry["1"];
    //build subscriber information used in api
    $mydata = "&data=email,first,last,custom_democustomfield^*$email,$name,TestLastName,Test#2";

    $postFields = $cmd . $myrealm . $mypass . $mylist . $myformat . $mytestoption . $mydata; //7. describes the order of parameters sent

    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 7); //8. number of fields again

    $result = curl_exec ($ch);
    curl_close ($ch);

    } else { return TRUE; }
    //print $result;
    }

  13. Rian says:

    What is I have a custom script file that does not email that I want it to POST to?