user2079954
user2079954

Reputation: 479

Passing a parameter form PHP HTML form

I am using WordPress and Gravity Forms plugin and I am trying to pass a parameter from a third party provider to a Gravity Form with Dynamic Population from another page, using the below code

<form method="post" name="goToForm" action="http://www.example.com/?page_id=123">
    <input type="hidden" name="param" value="Hello">
    <input type="submit" name="fromSubmit" value="Submit">
</form>

Note that the above http://www.example.com/?page_id=123 is the Gravity Form URL.

the closest solution I found is using the HOOK method, but still I want to know how can I call the custom function that is created in functions.php using the HOOK approach from post and pass the parameter.

Any suggestions will be appreciated

Upvotes: 0

Views: 2867

Answers (1)

designtocode
designtocode

Reputation: 2245

If I'm understanding you correctly you want to pass the parameters on the form url?

You can accomplish this in 2 ways:

URL: http://www.example.com/?page_id=123

  1. You can add a hidden field in the form. In the advanced section of the field, select Allow field to be populated dynamically and add the parameter name. So example I want to get the page_id:

enter image description here

After saving your form, inspect the hidden field and you should see it's value as 123

  1. You can add a hook function:

    add_filter('gform_field_value_page_id', 'my_custom_population_function'); function my_custom_population_function($value){ return $value'; //or do what ever you want with it }

If you want to add the page title or id automatically to the form:

  1. Add a hidden field, in the advanced section of the field, add this {embed_post:ID} (Post ID) to the default value. OR

  2. Add a hidden field, in the advanced section of the field, add this {embed_post:post_title} (Post Title) to the default value.

Edit

The user is looking for http://www.gravityhelp.com/documentation/page/Gform_after_submission

You can get your fields/parameters from your form and then save it to your database, update a Wordpress page/post or send it to a third party service provider.

I'm not too sure what user would like to do with the parameter, so I'll show an example of sending it to a third party provider:

  • We want our entry field numbers so we can get the correct fields:

    /* Getting correct field numbers */
    
    
    add_action("gform_after_submission", "post_to_third_party", 10, 2);
    
    function post_to_third_party($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('[email protected]', 'Getting the Gravity Form Field IDs', $message);
    } 
    

You should get something like this in your mail:

Array
    (
        [id] => 64
        [form_id] => 5
        [date_created] => 2014-07-02 13:27:00
        [is_starred] => 0
        [is_read] => 0
        [ip] => ::1
        [source_url] => http://localhost/
        [post_id] =>
        [currency] => USD
        [payment_status] =>
        [payment_date] =>
        [transaction_id] =>
        [payment_amount] =>
        [payment_method] =>
        [is_fulfilled] =>
        [created_by] => 1
        [transaction_type] =>
        [user_agent] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3)
    AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153
    Safari/537.36
        [status] => active
        [1] => Name
        [4] => Parameter
    )

Where [1] => Name is a Name field and I entered Name for testing and [4] => Parameter is the parameter field with a default value of Parameter.

  • After we have our correct field numbers, we can then submit it to the third party provider, I'm using curl in this example:

    /* Submitting to thirdparty.com */
    
    add_action("gform_after_submission", "post_to_third_party", 10, 2);
    
    function post_to_third_party($entry, $form){
    
    
    //Submitting to thirdparty.com using curl
    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);
         curl_setopt($post, CURLOPT_HEADER, 1); //if you want headers
         curl_setopt($post, CURLOPT_HEADER, "Content-Type:application/xml");
         $result = curl_exec($post); 
    
         //If there's an error
    
         if($result === false)
            {
                echo "Error Number:".curl_errno($ch)."<br>";
                echo "Error String:".curl_error($ch);
         }
    
         curl_close($post);
    }
    
    if($form["id"] == 1){//Form ID
        //Lets get the fields to match submission to thirdparty.com
        $data = array(
             "FirstName" =>     $entry["1"],
             "ParameterName" =>     $entry["4"]
        );
    
            post_to_url("http://thirdparty.com", $data);
    }
    
    }

If you want the hook to work for a specific form gform_after_submission_1 will work for form id 1 only.

Upvotes: 2

Related Questions