PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Help With gform_after_submission

  1. I was wondering if someone can help me to understand how the gform_after_submission works.

    I created a sample Gravity Form with the id gform_1
    I added one Text Field <input name="input_1" id="input_1_1" type="text" value="" class="medium" tabindex="1">

    To have that text fields info passed to a php scrict called dataprocess.php I added this to my functions.php file:

    <?php
    add_action('gform_after_submission_1', 'post_to_third_party', 10, 2);
    function post_to_third_party($entry, $form) {
    
        $post_url = 'dataprocess.php';
        $body = array(
            'bname' => $entry['input_1_1'],
            );
    
        $request = new WP_Http();
        $response = $request->post($post_url, array('body' => $body));
    
    }
    ?>

    In my dataprocess.php I added:

    <?php
    $bname = $_POST['bname'];
    echo "Hello " . $bname . "";
    ?>

    Should the form automatically redirect me to the dataprocess.php or do I need to setup the redirect and are the variables correct the way I am using them. I don't seem to get the php file to echo anything back to me.

    Thanks for any help

    Posted 12 years ago on Wednesday April 11, 2012 | Permalink
  2. Hi RevRandy,

    Rather than "post" your data to your own server again, I'd recommend just completing the data processing form within your "post_to_third_party" function. The way you currently have it setup will not work. If you share your end goal we can give you some better tips on how to get there. :)

    Posted 12 years ago on Wednesday April 11, 2012 | Permalink
  3. Hi David,

    Thank you for your reply. What I am trying to do is pass information from a gravity form to the First Data Payment Gateway.

    The Payment Gateway requires certain field names to be passed like bname, baddr1, bcity, etc..

    My thoughts were to use the gform_after_submission to pass the info to a php script and make the required changes and submit the data from there to the payment gateway.

    I was not really sure of the actual process and whether I could accomplish this with the hook or not.

    I think adding it to the functions.php makes good sense if I can get some assistance in understanding the correct way to use the hook to change the field names I may be able to make it work.

    Thanks again for your assistance.

    Posted 12 years ago on Wednesday April 11, 2012 | Permalink
  4. Hi RevRandy,

    The first snippet is somewhat close to what you're shooting for; however, you don't need to send it to a separate PHP file to update the values you'd like to modify. You can do that right from within the post_to_third_party() function.

    add_action('gform_after_submission_1', 'post_to_third_party', 10, 2);
    function post_to_third_party($entry, $form) {
    
        $post_url = 'http://yourthirdparty.com/path/to/api/';
    
        $body = array(
            'full_name' => $entry[1] . ' ' . $entry[2]
            );
    
        $request = new WP_Http();
        $response = $request->post($post_url, array('body' => $body));
    
    }

    In this example, I'm join the values for field id #1 and #2 and passing to my third party as "full_name".

    Posted 12 years ago on Wednesday April 11, 2012 | Permalink
  5. So the field name I need to pass is bname I would use:

    'bname' => $entry[1] . ' ' . $entry[2] );

    the $post_url = 'http://yourthirdparty.com/path/to/api/' would be the url to the paypent gateway

    Do the numbers for the $entry change to $entry[input_1_1] ? I am not sure where the 1 and 2 are coming from.

    Posted 12 years ago on Wednesday April 11, 2012 | Permalink
  6. One other thing I think that may be a huge problem. The way the payment gateway is set up is that we send the form info to the payment gateway, name, address, phone. city, zip, state. They are redirected to the payment gateway with those fields already filled in and then we enter the payment info.

    Is it possible to pass this info to the gateway in the same manor as a form or no.

    Posted 12 years ago on Wednesday April 11, 2012 | Permalink
  7. After some more testing I am getting closer put things still don't seem to be working right.

    I am able to get the correct data from the fields and place them in the array with the key being the field name required by the payment gateway.

    ~add_action('gform_after_submission_4', 'post_to_third_party', 10, 2);
    function post_to_third_party($entry, $form) {

    $post_url = 'https://www.linkpointcentral.com/lpc/servlet/lppay';

    $body = array(
    'bname' => $entry['10'], 'baddr1' => $entry['2.1'] );

    $request = new WP_Http();
    $response = $request->post($post_url, array('body' => $body));
    }~

    How can I pass these variables and redirect the way a form would redirect to the payment gateway.

    Thanks again for your assistance.

    Posted 12 years ago on Thursday April 12, 2012 | Permalink
  8. I have all the $body = array fields assigned and working correctly. I added a additional 'submit' => 'submit' to the array as submit did not have a name=.

    The form still does not redirect to the payment gateway but remains on the page the form is on. Is there something else I may need to add to the code to have it post to the payment gateway.

    I double checked a html from I made with required fields and it will post so I am pretty sure I have all the required fields.

    Thanks for your assistance

    Posted 12 years ago on Thursday April 12, 2012 | Permalink
  9. I do know if I echo $response it just says Array

    Posted 12 years ago on Thursday April 12, 2012 | Permalink
  10. I'm also interested in this question. I have added a gform_after_submission function which I'm fairly sure is accurate, but the form does not go to the payment gateway. What setting am I missing? Could you explain a little more specifically what that function accomplishes?

    Posted 12 years ago on Tuesday July 3, 2012 | Permalink
  11. David Peralty

    gform_after_submission doesn't control redirects without having code to redirect people. The function passes data behind the scenes.

    See this post:
    http://planetozh.com/blog/2009/08/how-to-make-http-requests-with-wordpress/

    Hopefully, it should help you understand better what people are doing with WP_Http;

    Posted 12 years ago on Tuesday July 3, 2012 | Permalink
  12. I have a form which submits to a redirect page. Based on a yes/no answer in a radio choice field ("Are you in need of financial aid?", the form is redirected either to a financial aid application or the payment gateway ( https://www.linkpointcentral.com/lpc/servlet/lppay ). I've set up the gform_after_submission post_to_third_party function to post to that same URL. But when I submit the form, it submits to https://www.linkpointcentral.com/lpc/servlet/lppay#gf_2. Why is it appending the id of the form to the URL?

    Code for redirect template and post_to_third_party() is here:
    http://pastebin.com/9iLhRpmR

    Posted 12 years ago on Tuesday July 3, 2012 | Permalink
  13. Okay, I've figured out why #gf_2 was appended to the URL -- it's because I was testing from the Form Preview instead of from the page that the form is on. Duh.

    But testing from the form on the published page still results in no post variables being passed, as far as I can tell.

    Posted 12 years ago on Tuesday July 3, 2012 | Permalink
  14. David Peralty

    Have you checked with linkpointcentral to see if they are getting any data? Have you tried printing out the $result['response'] to see if things worked okay? Do they have an API that allows a response code through this method?

    Posted 12 years ago on Tuesday July 3, 2012 | Permalink
  15. Here's the problem as I see it: I need to submit the main registration form to a redirect page which then sends the form to either a financial aid form or the payment gateway. I can't use GET variables because I need to specify the URLs on the redirect page. So I need to pass POST variables. That seems to mean that I need to use the gform_after_submission post_to_third_party function in the Documentation here:
    http://www.gravityhelp.com/documentation/page/Gform_after_submission
    But I can't get the $body array variables to pass.

    On my test site, I've tried ditching the redirect page and submitting the form to a page on my own test site where I can ask for variables and check whether they're coming through. But they're not. Adding
    print_r ($result['response']);
    to the function results in nothing.

    A GET variable comes through okay, but that doesn't work for a redirect page, as detailed above.

    Sometimes I can catch this:
    Array ( [chargetotal] => 32 [mode] => Fullpay [storename] => 1001294132 [txntype] => sale )
    but I don't know what to do with it :)

    Posted 12 years ago on Wednesday July 4, 2012 | Permalink
  16. Okay, I've simplified this a bunch. No more redirect, just a straight form submitted to another page on the site. I'll submit to the payment gateway from there. I have the following code to gather the post variables:

    add_action('gform_after_submission', 'post_to_third_party', 10, 2);
    function post_to_third_party($entry, $form) {
    
        $post_url = 'another-page-on-the-site';
        $body = array(
                'first_name' => $entry['1.3'],
                'last_name' => $entry['1.6'],
    	    'chargetotal' => $entry['5'],
    	    'test' => 'test',
            );
    	if( !class_exists( 'WP_Http' ) )
    	include_once( ABSPATH . WPINC. '/class-http.php' );
    
    	$request = new WP_Http();
        $result = $request->post($post_url, array('body' => $body));
    }

    This line on the receiving page
    var_dump($result['response']);
    results in a NULL
    and none of the $body variables prints, not even the hard-coded test variable.

    Any idea why the function is not working?

    Posted 12 years ago on Wednesday July 4, 2012 | Permalink
  17. David Peralty

    It isn't working because it doesn't work the way you think it does. What is happening is that behind the scenes, WordPress is now sending that data to your other page, and waiting for that page to send a response.

    You are expecting it to work like a normal POST call, like if you had changed where the form submits to, but that's not how this works.

    That is why you were receiving data back sometimes before in your previous attempt because where you were submitting the data was receiving it, and sending a result back. Now, you are sending to another page, and it probably won't receive a response back.

    Did you read through the article I posted earlier?
    http://planetozh.com/blog/2009/08/how-to-make-http-requests-with-wordpress/

    Some people call this using an API. You send an external site or service some data, and they send back details so that you can then use it or whatnot.

    So when you had

    Sometimes I can catch this:
    Array ( [chargetotal] => 32 [mode] => Fullpay [storename] => 1001294132 [txntype] => sale )
    but I don't know what to do with it :)

    You were sending data to the service, and those were the details it sent back to you.

    You could then echo out a message to users letting them know that things worked, or you can ignore this response because things are working. Either way, this was working how it is intended.

    Users will not be redirected anywhere, values will not show up on another page. The function you are working with sends data behind the scenes.

    Posted 12 years ago on Wednesday July 4, 2012 | Permalink