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.

Using Cookies to populate forms

  1. Hi All,

    I'm attempting to set cookies to populate a form based on this thread: http://www.gravityhelp.com/forums/topic/little-hint-from-me-save-user-input-in-cookie-and-prepopulate-form

    I've hit a dead-end when it comes to having the cookie set on multi-input fields. The cookie sets just fine if the filed has a single value (email, phone, etc.) but when it comes to fields like Name (first and last) and Address (Address 1, Address 2, City, State, Zip, Country) the cookie just isn't setting at all.

    Here's the code that I'm using to set the cookies:

    add_action("gform_pre_submission", "pre_submission_handler");
    function pre_submission_handler($form_meta) {
    	$saveVars = array("fname", "lname", "address", "addtwo", "phone", "email", "country", "state", "city", "zip");
    
        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);
    		}
    	}
    setcookie("gf_form_meta", serialize($form_meta['fields']), time() + 31536000, COOKIEPATH, COOKIE_DOMAIN, false, true);
    
    }

    Here are the filters:

    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_address", "populate_address");
    function populate_address() {
    	return $_COOKIE["gf_address"];
    }
    add_filter("gform_field_value_addtwo", "populate_addtwo");
    function populate_addtwo() {
    	return $_COOKIE["gf_addtwo"];
    }
    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_city", "populate_city");
    function populate_city() {
    	return $_COOKIE["gf_city"];
    }
    add_filter("gform_field_value_zip", "populate_zip");
    function populate_zip() {
    	return $_COOKIE["gf_zip"];
    }

    I do have the fields set to populate dynamically and am passing the field parameters correctly (fname, lname, etc.)

    Has anyone successfully gotten this to work? Any thoughts on how to pull it off?

    Posted 12 years ago on Friday April 6, 2012 | Permalink
  2. Hi - I'm sure you have this figured out by now, but cwidhelm on the other thread posted a great anonymous function to iterate all dynamic-enabled fields.

    I've copied it below - copy this into your functions.php. Then just edit your form in GF, and set a unique string for the dynamic parameter for each field.

    function pre_submission_handler($form_meta) {
    	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);
    			}
    		}
    	}
    	setcookie("gf_form_meta", serialize($form_meta["fields"]), time() + 31536000, COOKIEPATH, COOKIE_DOMAIN, false, true);
    }
    add_action("gform_pre_submission", "pre_submission_handler");
    
    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;
    }
    add_filter("gform_pre_render", "add_auto_update_filters");
    Posted 11 years ago on Friday September 7, 2012 | Permalink
  3. Thanks for posting that code @Twig.

    Posted 11 years ago on Friday September 7, 2012 | Permalink
  4. 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 11 years ago on Friday January 11, 2013 | Permalink

This topic has been resolved and has been closed to new replies.