Passing values from form to another form
I am looking for a solution to pass values from a contact form to the next contact form. I think a good way is to show you an example: https://www.zonnepanelen-weetjes.nl/ when you type in a postal code (for example: 2012 ES) it’ll pass the rest of the info to the next page https://www.zonnepanelen-weetjes.nl/offerte-formulier/?postalcode=2012+ES My idea is to have something similar but with the Place Autocomplete Address from Google. https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform I know how to implement this function into my plugin (contact form 7) for WordPress What I would like is on the first form you can enter the full address with Google autocomplete. Then onto the next form it’ll pass the info into their separate fields. As shown in the above link from Google. I hope someone can point me in the right direction here. I have some basic knowledge of PHP and could perhaps change some of the code the plugin to make this work. Thanks in advance and I hope I explained myself well enough as I do not know how I would describe this function otherwise. Greetings, Mark —EDIT—
SOLUTION As I was looking through the internet I found my own solution to this. I will write the solution here for future reference so other people can read. You will need these 2 plugins to be installed: https://wordpress.org/plugins/wpcf7-redirect/
https://wordpress.org/plugins/contact-form-7-dynamic-text-extension/ In form 1 you can add for example
In form 1 go to Redirect Settings and select a page to redirect to on successful form submission. Then either select one of these:
Pass all the fields from the form as URL query parameters
Pass specific fields from the form as URL query parameters I selected the first one but it will send all parameters, the other one lets you select specific fields but for this demo purpose I passed all fields. Now in form 2 do this:
passing parameters to php in a form?
Caution. DO NOT print out unformatted values (e.g. echo $_GET[‘pk’]) to a page! It is a security hole which introduces a world of issues (XSS just to name one). If «pk» is the primary key of a database and it is a number, you can either sanitize with a regex or with the intval() function
I won’t be printing it out to a page, it will be passed to up.php where it will be inserted into a database via a parametrized query Why is there a need to sanitize it in transit?
4 Answers 4
By the way, printing large amounts of HTML like you have there is ugly. Consider either stepping out of PHP to do so, using HEREDOC, a template engine, or a framework.
As noted below, you should not print GET and POST data back to the page without sanitizing it first. Assuming pk is a primary key, you should wrap $pk above with the intval function, at the very least.
Sigh. Yeah, yeah, I know. I can only say it so many times in answers here until I stop caring. Edited to reflect this.
I agree with all the comments regarding some kind of input control of the $_GET[‘pk’] variable. I would recommend the filter module in php, which is pretty much a default installed module I believe.
You can find more information about the filter module here: link text
I also agree with Paolo Bergantino, this is not the prettiest way to do it, and a template engine, heredocs or regexp could be a better way of increasing the readability and maintainability of the system.
Passing a parameter form PHP HTML 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
1 Answer 1
If I’m understanding you correctly you want to pass the parameters on the form url?
You can accomplish this in 2 ways:
- 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:
After saving your form, inspect the hidden field and you should see it’s value as 123
- 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:
- Add a hidden field, in the advanced section of the field, add this (Post ID) to the default value. OR
- Add a hidden field, in the advanced section of the field, add this (Post Title) to the default value.
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('you@domain.com', '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)."
"; 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.