Please Note: We are no longer using forums to facilitate standard support. 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.

Save user input in cookie and prepopulate form

  1. aibon
    Member

    we have multiple forms on our website, on most of them people will have to enter name, phone and email. to make this more userfriendly, here's a snippet on how to store these values in cookies and prepopulate the forms with it.

    to make this work you'll have to check 'allow field to be populated dynamically' and set the parameter name accordingly (in this example 'name', 'phone' and 'email')

    here's the script, that should go in your functions.php

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta) {
    	$saveVars = array("name", "email", "phone");
    
        foreach($form_meta["fields"] as $field) {
    		if (in_array($field["inputName"], $saveVars)) {
    			setcookie("gf_".$field["inputName"], $_POST["input_" . $field["id"]], time() + 31536000, COOKIEPATH, COOKIE_DOMAIN, false, true);
    		}
    	}
    }
    
    add_filter("gform_field_value_name", "populate_name");
    function populate_name() {
    	return $_COOKIE["gf_name"];
    }
    add_filter("gform_field_value_phone", "populate_phone");
    function populate_phone() {
    	return $_COOKIE["gf_phone"];
    }
    add_filter("gform_field_value_email", "populate_email");
    function populate_email() {
    	return $_COOKIE["gf_email"];
    }

    hope this helps someone...

    Posted 4 years ago on Sunday November 21, 2010 | Permalink
  2. mmtrav
    Member

    Brilliant! Just what I was looking for..

    Posted 4 years ago on Tuesday December 14, 2010 | Permalink
  3. mmtrav
    Member

    ... until I couldn't get it to work properly.

    First off, I can't seem to see that the cookie is being saved, unless it becomes a PHPSession ID cookie in which case it looks like it's encoded somehow.

    Second, I think that my dynamically populated field names aren't working out correctly. I need to use slightly different names and I might not be changing the right ones in this code snippet.

    Does this code snippet make every form save all the data in cookie? Or does it target a particular form?

    Posted 4 years ago on Tuesday December 14, 2010 | Permalink
  4. @mmtrav This code snippet looks like it would apply to all forms. You would have to customize it so that it was form specific using an if statement around the code that is executed.

    Posted 4 years ago on Tuesday December 14, 2010 | Permalink
  5. Any chance there's a slightly more detailed version of this demonstrated somewhere?

    I'm having a hard time figuring out how and what exactly it is that I need to change to collect the data from the fields I want to prepopulate.

    Posted 4 years ago on Thursday February 10, 2011 | Permalink
  6. So trick number one was enabling the prepopulation in the fields first (which is where I was able to enter the keys I wanted to use, rather than trying to figure out where the heck to get them from so I could enter them later). Helps to really read the instructions.

    I almost have this working just right. Everything other than first name and last name are working. I'm assuming this has something to do with the fact that they're the same field but split into two parts. I do need to keep them separate and not the simple entry field.

    Any thoughts on how to amend this code to resolve the issue?

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta) {
    	$saveVars = array("fname", "lname", "title", "company", "phone", "email", "country", "state", "sales");
    
        foreach($form_meta["fields"] as $field) {
    		if (in_array($field["inputName"], $saveVars)) {
    			setcookie("gf_".$field["inputName"], $_POST["input_" . $field["id"]], time() + 31536000, COOKIEPATH, COOKIE_DOMAIN, false, true);
    		}
    	}
    }
    
    add_filter("gform_field_value_fname", "populate_fname");
    function populate_fname() {
    	return $_COOKIE["gf_fname"];
    }
    add_filter("gform_field_value_lname", "populate_lname");
    function populate_lname() {
    	return $_COOKIE["gf_lname"];
    }
    add_filter("gform_field_value_title", "populate_title");
    function populate_title() {
    	return $_COOKIE["gf_title"];
    }
    add_filter("gform_field_value_company", "populate_company");
    function populate_company() {
    	return $_COOKIE["gf_company"];
    }
    add_filter("gform_field_value_phone", "populate_phone");
    function populate_phone() {
    	return $_COOKIE["gf_phone"];
    }
    add_filter("gform_field_value_email", "populate_email");
    function populate_email() {
    	return $_COOKIE["gf_email"];
    }
    add_filter("gform_field_value_country", "populate_country");
    function populate_country() {
    	return $_COOKIE["gf_country"];
    }
    add_filter("gform_field_value_state", "populate_state");
    function populate_state() {
    	return $_COOKIE["gf_state"];
    }
    add_filter("gform_field_value_sales", "populate_sales");
    function populate_sales() {
    	return $_COOKIE["gf_sales"];
    }
    Posted 4 years ago on Thursday February 10, 2011 | Permalink
  7. Oh, and enormous thanks to @aibon for sharing this to begin with. Big help.

    Posted 4 years ago on Thursday February 10, 2011 | Permalink
  8. You should be able to populate the name field and other multi-input fields (like Address) using the same code. When you set a Name field so it can be populated dynamically it lets you give each element a parameter name. See this screenshot:

    http://grab.by/8SjI

    You then target those inputs using the parameter name you configured for each.

    Posted 4 years ago on Thursday February 10, 2011 | Permalink
  9. Yeah, that's what I have done, but for some reason it's not holding those values when I try another form on the site. Every other field works fine, but not that one.

    Posted 4 years ago on Thursday February 10, 2011 | Permalink
  10. Have you tried just outputting that session variable on the page where it would be visible to see if it even has a value? Could be something wrong with setting the value rather than returning the value.

    Posted 4 years ago on Thursday February 10, 2011 | Permalink
  11. Checked the cookie and you're quite right that it's not setting the value. Unfortunately that still leaves me not knowing quite how to fix it as I don't really understand the setcookie parameters. I'm going to keep mucking about with it, but any help you can provide is much appreciated. Thanks Carl.

    Posted 4 years ago on Thursday February 10, 2011 | Permalink
  12. mrhoneyfoot
    Member

    Subscribed.

    Posted 4 years ago on Thursday February 10, 2011 | Permalink
  13. Did you ever get this resolved? I'm having the same problem.

    Posted 4 years ago on Monday May 9, 2011 | Permalink
  14. I used this method to get my fields pre-populated and it does work, but there is a problem...someone else is on the form page from a different computer and it pre-populates the last persons information who submitted.

    The form I have is a sidebar form http://elderhomefinders.com. If you fill that information out on the side it takes you to the other form which pre-populates but not with the information you just submitted but with the person who previous submitted a form.

    Do you know how to fix this?

    Posted 3 years ago on Monday June 6, 2011 | Permalink
  15. Hi Taurad,

    It seems like you've got something else going on here. PHP Cookies are specific to the users browsers and current session. Are you using any other methods to attempt to pre-populate these fields?

    Posted 3 years ago on Monday June 6, 2011 | Permalink
  16. No, I tried the query string but couldn't seem to figure it out. Do you have any suggestions?
    I have the fields marked to dynamically populate on those 4 fields on both forms, should I only have that box checked on the second form?

    Posted 3 years ago on Monday June 6, 2011 | Permalink
  17. Do you have a detailed example of how to populate using the query string?

    Posted 3 years ago on Thursday June 9, 2011 | Permalink
  18. @taurad659 Here is a detailed tutorial that covers dynamic population of fields:

    http://www.gravityhelp.com/documentation/page/Using_Dynamic_Population

    Posted 3 years ago on Thursday June 9, 2011 | Permalink
  19. I just tried this myself and company,email and phone worked but the fname and lname did not. both are what i use for pre-populating via a link. This is the script i have for the name section.

    add_filter("gform_field_value_fname", "populate_fname");
    function populate_fname() {
    	return $_COOKIE["gf_fname"];
    }
    add_filter("gform_field_value_lname", "populate_lname");
    function populate_lname() {
    	return $_COOKIE["gf_lname"];
    Posted 3 years ago on Wednesday September 21, 2011 | Permalink
  20. @Scott See the user's comments who posted this above. The issue is somewhere in his code. He didn't post a follow up with the resolution. His First and Last Name didn't work either because of the code issue setting the cookie.

    Posted 3 years ago on Wednesday September 21, 2011 | Permalink
  21. Understandable. Hoping that someone can come up with a solution because this idea is awesome!

    Posted 3 years ago on Wednesday September 21, 2011 | Permalink
  22. can i add a check box in the end of every form that the viewer can mark if he want to save the info for other form or not?

    Posted 3 years ago on Tuesday January 24, 2012 | Permalink
  23. This is a great thread. Thanks for keeping it going. I would love to see this built into Gravity Forms. This is VERY useful functionality if you're using Gravity Forms for a lot of lead forms on your site and you don't want people to have to enter their info multiple times on different forms.

    Posted 3 years ago on Tuesday March 27, 2012 | Permalink
  24. Hi Carl,

    Yes the session variable is not outputting. So there is a mistake with setting the value but how should i get it working? Tried with different parameter_name for first and last name field. Same issue with address fields, dropdown menu and radio buttons.

    Any suggestions/guidelines will be great.

    Thanks!

    Posted 3 years ago on Monday April 2, 2012 | Permalink
  25. Thanks everyone for this. This is really a great solution.

    I'm also having trouble setting fname and lname as well as address fields. It looks to me that there is an issue setting with multi-input fields, but I think everyone above already figured that out.

    I'd love to see a resolution if anyone comes up with one.

    Thanks!

    Posted 3 years ago on Thursday April 5, 2012 | Permalink
  26. In case anyone wants to try this out. It will auto populate any field that is marked as "Allow field to be populated dynamically" with it's previous value from a cookie.

    Add this to your functions.php

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta) {
            $saveVars = array("first", "last", "email", "phone");
            foreach($form_meta["fields"] as $field) {
                    if( $field["allowsPrepopulate"] ){
                            if( is_array($field["inputs"]) ){
                                    foreach($field["inputs"] as $sub){
                                            $val = $_POST["input_" . str_replace(".", "_", $sub["id"])];
                                            setcookie("gf_".$sub["name"], $val, time() + 31536000, COOKIEPATH, COOKIE_DOMAIN, false, true);
                                    }
                            }else{
                                    $val = $_POST["input_" . $field["id"]];
                                    setcookie("gf_".$field["inputName"], $val, time() + 31536000, COOKIEPATH, COOKIE_DOMAIN, false, true);
                            }
                    }
            }
    }
    
    add_filter("gform_pre_render", "add_auto_update_filters");
    $contego_callbacks = array();
    function add_auto_update_filters($form){
            foreach($form["fields"] as &$field){
                    if( $field["allowsPrepopulate"] ){
                            if( is_array($field["inputs"]) ){
                                    foreach($field["inputs"] as $sub){
                                            $fieldName = $sub["name"];
                                            add_filter("gform_field_value_" . $fieldName, function($fieldName) use ($fieldName){
                                                    return $_COOKIE["gf_" . $fieldName];
                                            });
                                    }
                            }else{
                                    $fieldName = $field["inputName"];
                                    add_filter("gform_field_value_" . $fieldName, function($fieldName) use ($fieldName){
                                            return $_COOKIE["gf_" . $fieldName];
                                    });
                            }
    
                    }
            }
            return $form;
    }
    Posted 3 years ago on Wednesday May 2, 2012 | Permalink
  27. That is really nice. I tried it out, but it put the email field in the first, last and email fields in the form. Is that maybe an issue with how the cookie is being set?

    Posted 2 years ago on Monday December 3, 2012 | Permalink
  28. Can you share a link to the page where the form is embedded so we can check it out?

    Posted 2 years ago on Monday December 3, 2012 | Permalink
  29. FYI, in order to get this working on my server (it was causing 500 errors) I have had to remove the & from before $field

    foreach($form["fields"] as &$field){

    to

    foreach($form["fields"] as $field){

    Hope that helps

    Posted 2 years ago on Friday January 11, 2013 | Permalink
  30. Thank you for posting that. I believe you can no longer use that notation. In PHP version 5.4 and later it will raise a fatal error. In PHP 5.3, it will raise a warning. What PHP version are you using?

    Posted 2 years ago on Friday January 11, 2013 | Permalink

 

Support Tickets

Open A New Support Ticket
 Open A New Priority Support Ticket *

* Developer license holders only