Conditionally Cookies save after checkbox click

Hello,
I would like to ask you for advice about a form I have recently built. This is the URL where you can check it - http://cloud.tandet.com/~shipnamerica/request-a-quote/ .
There is a checkbox field - “Save my contact information for future Freight Rate Quotes” which should be saving Cookies and fields be auto-populated after the form is once submitted.

At this moment I managed to enable Cookies for the form which are saved once the form is submitted but they should be populated only in a condition after this checkbox is clicked.
Can you please advice me for a way of doing this.

The code which I used to make this auto-fill after form submission was from this thread - https://legacy.forums.gravityhelp.com/topic/little-hint-from-me-save-user-input-in-cookie-and-prepopulate-form

Hope you can give me advice about setting up this feature with the checkbox.
Thank you.
Kind regards.

What is the code you are using to set the cookie now? If you share that, we might be able to help you check for that checkbox value to conditionally.

Inside the pre_submission_handler there should be a check if the cookie-checkbox (to call it that :wink: ) has been selected and if not: return.

Something along these lines:

function pre_submission_handler($form_meta) {
    if ( false == $_POST['input_68'] ) return;
    ....

Hello Chris and Hiranthi,
Thank you for your advices. I appreciate it.
The code which is currently applied in my functions.php is:

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"];
}

Best Regards, Nick.

Combining Hiranthi’s code with yours, this should work:

add_action("gform_pre_submission", "pre_submission_handler");
function pre_submission_handler($form_meta) {
	if ( false == $_POST['input_68'] ) return;
	$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);
		}
	}
}

That just checks to see if field 68 (your checkbox field) was submitted, and if not, skip the code to set the cookies.

1 Like

Hello. Thanks so much for the efforts to help solve this.
I updated with the code above but is not saving cookies after the checkbox is clicked.
The attached is the code from my functions.php.

Best Regards, Nick.
Thank you.

In the above code, I recommend adding a logging line, like this:

add_action("gform_pre_submission", "pre_submission_handler");
function pre_submission_handler($form_meta) {
	GFCommon::log_debug( __METHOD__ . '(): POST => ' . print_r( $_POST, true ) );
	if ( false == $_POST['input_68'] ) return;

That will only be helpful if you enable Gravity Forms logging:

The $_POST will be logged and see can see what is happening and determine if the logic is just plain wrong on something like that.

Hello, thank you once again for trying to help my situation!
I enabled logging and replaced with the code provided above.
This is what I got from the log document:

==========================================

2019-03-28 9:45:38.045504 - DEBUG --> GFCommon::post_to_manager(): endpoint: https://www.gravityhelp.com/wp-content/plugins/gravitymanager/api.php?op=upgrade_message&key=0c226b1ae0ce43b4c7100e1f679c3ac1 
2019-03-28 9:45:38.252562 - DEBUG --> GFCommon::log_remote_response(): code: 200; body: <!--GFM--> 
2019-03-28 9:47:15.034734 - DEBUG --> GFFormDisplay::process_form(): Starting to process form (#2) submission. 
2019-03-28 9:47:15.043602 - DEBUG --> GFFormDisplay::process_form(): Source page number: 1. Target page number: 0. 
2019-03-28 9:47:15.050630 - DEBUG --> GFFormDisplay::process_form(): After validation. Is submission valid? Yes. 
2019-03-28 9:47:15.050802 - DEBUG --> GFFormDisplay::process_form(): Submission is valid. Moving forward. 
2019-03-28 9:47:15.051012 - DEBUG --> pre_submission_handler(): POST => Array
(
    [input_1] => Nikolay
    [input_2] => Birimski
    [input_3] => Hyperweb Test
    [input_4] => 
    [input_5] => Hyperweb Test
    [input_6] => Yukon
    [input_7] => 
    [input_8] => 08888888
    [input_9] => 
    [input_10] => nicky@hyperweb.ca
    [input_11] => nicky@hyperweb.ca
    [input_68_1] => 1
    [input_68_2] => Save my contact information for future Freight Rate Quotes
    [input_68_3] => 1
    [input_20] => Hyperweb Test
    [input_22] => Arkansas
    [input_24] => Business
    [input_28] => Hyperweb Test
    [input_29] => Alaska
    [input_30] => Business
    [input_34] => Truckload
    [input_50] => Standard Service
    [input_51] => 53\' High Cube Dry Van
    [input_52] => Yes
    [input_53] => 1
    [input_54] => Kilos
    [input_57] => No
    [input_56] => 
    [input_58] => Actual
    [input_59] => Array
        (
            [0] => 15
            [1] => 3
            [2] => 2019
        )

    [input_60] => Hyperweb Test
    [input_63_2] => Yes! Please sign me up for your free newsletter.
    [input_63_3] => 1
    [is_submit_2] => 1
    [gform_submit] => 2
    [gform_unique_id] => 
    [state_2] => WyJ7XCI2OC4xXCI6XCI5OWU3MjEzZjgyZGM3ZGIzYmY0ZTc3YWQ1OWRkMTdiZVwiLFwiNjguMlwiOlwiZGVkOGEwNTFkNjZhNjNkOTllMDY1YmZhMjhmZjllNjRcIixcIjY4LjNcIjpcIjk5ZTcyMTNmODJkYzdkYjNiZjRlNzdhZDU5ZGQxN2JlXCIsXCI2My4xXCI6XCI5OWU3MjEzZjgyZGM3ZGIzYmY0ZTc3YWQ1OWRkMTdiZVwiLFwiNjMuMlwiOlwiNjlkMTBmNTI1NzcxOGVjZmRlZjBlNDI1MjY2NGIwMTFcIixcIjYzLjNcIjpcIjk5ZTcyMTNmODJkYzdkYjNiZjRlNzdhZDU5ZGQxN2JlXCJ9IiwiNmM3OGI3MmExZTg3OWI2YjU3MGNmYjZlNDA3NmY2NzUiXQ==
    [gform_target_page_number_2] => 0
    [gform_source_page_number_2] => 1
    [gform_field_values] => 
)
 
2019-03-28 9:47:15.051192 - DEBUG --> GFFormsModel::save_entry(): Saving entry. 
2019-03-28 9:47:15.052424 - DEBUG --> GFFormsModel::save_entry(): Entry record created in the database. ID: 11. 
2019-03-28 9:47:15.052932 - DEBUG --> GFFormsModel::save_entry(): Saving entry fields. 
2019-03-28 9:47:15.053294 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#1 - text). 
2019-03-28 9:47:15.053599 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#2 - text). 
2019-03-28 9:47:15.053874 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#3 - text). 
2019-03-28 9:47:15.054152 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#4 - text). 
2019-03-28 9:47:15.054442 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#5 - text). 
2019-03-28 9:47:15.054723 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: State/Province(#6 - select). 
2019-03-28 9:47:15.055032 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#7 - text). 
2019-03-28 9:47:15.055319 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#8 - text). 
2019-03-28 9:47:15.055610 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#9 - text). 
2019-03-28 9:47:15.055886 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#10 - text). 
2019-03-28 9:47:15.056159 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#11 - text). 
2019-03-28 9:47:15.056469 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#68.1 - consent). 
2019-03-28 9:47:15.056694 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#68.2 - consent). 
2019-03-28 9:47:15.056906 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#68.3 - consent). 
2019-03-28 9:47:15.057188 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Origin Fieldset(#18 - WrapperBegin). 
2019-03-28 9:47:15.057487 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#20 - text). 
2019-03-28 9:47:15.057780 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: State/Prov.(#22 - select). 
2019-03-28 9:47:15.058159 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#24 - radio). 
2019-03-28 9:47:15.058475 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#25.1 - checkbox). 
2019-03-28 9:47:15.058676 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#25.2 - checkbox). 
2019-03-28 9:47:15.058861 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#25.3 - checkbox). 
2019-03-28 9:47:15.059143 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Origin Fieldset(#19 - WrapperEnd). 
2019-03-28 9:47:15.059443 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Destination Fieldset(#26 - WrapperBegin). 
2019-03-28 9:47:15.059736 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#28 - text). 
2019-03-28 9:47:15.060029 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: State/Prov.(#29 - select). 
2019-03-28 9:47:15.060432 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#30 - radio). 
2019-03-28 9:47:15.060965 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#31.1 - checkbox). 
2019-03-28 9:47:15.061182 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#31.2 - checkbox). 
2019-03-28 9:47:15.061398 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#31.3 - checkbox). 
2019-03-28 9:47:15.061701 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Destination Fieldset(#27 - WrapperEnd). 
2019-03-28 9:47:15.062005 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: TL (Truckload) or LTL (Less than Truckload)?(#34 - select). 
2019-03-28 9:47:15.063543 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Service Required(#50 - select). 
2019-03-28 9:47:15.063893 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Equipment Required(#51 - select). 
2019-03-28 9:47:15.064239 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: On Pallets ?(#52 - select). 
2019-03-28 9:47:15.064611 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Total Weight:(#53 - text). 
2019-03-28 9:47:15.064991 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Lbs./ Kilos(#54 - select). 
2019-03-28 9:47:15.065385 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Any Hazardous Material ?(#57 - radio). 
2019-03-28 9:47:15.065709 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: If Hazmat, Please Enter Class/U.N. #:(#56 - text). 
2019-03-28 9:47:15.066061 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Shipment Pickup Date:(#58 - radio). 
2019-03-28 9:47:15.066462 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Select a Shipping Date:(#59 - date). 
2019-03-28 9:47:15.066822 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: General Description of Goods/Special Instructions:(#60 - textarea). 
2019-03-28 9:47:15.067152 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Please Contact Me By:(#61.1 - checkbox). 
2019-03-28 9:47:15.067397 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Please Contact Me By:(#61.2 - checkbox). 
2019-03-28 9:47:15.067598 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: Please Contact Me By:(#61.3 - checkbox). 
2019-03-28 9:47:15.067914 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#63.1 - consent). 
2019-03-28 9:47:15.068130 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#63.2 - consent). 
2019-03-28 9:47:15.068391 - DEBUG --> GFFormsModel::queue_save_input_value(): Queued field operation: (#63.3 - consent). 
2019-03-28 9:47:15.075649 - DEBUG --> GFFormsModel::save_entry(): Finished saving entry fields. 
2019-03-28 9:47:15.075942 - DEBUG --> GF_Background_Process::dispatch(): Running for gf_feed_processor. 
2019-03-28 9:47:15.076766 - DEBUG --> GF_Background_Process::dispatch(): Unable to dispatch tasks to Admin Ajax: Nothing left to process 
2019-03-28 9:47:15.077013 - DEBUG --> GFFormsModel::create_post(): Starting. 
2019-03-28 9:47:15.079967 - DEBUG --> GFFormsModel::create_post(): Stopping. The form doesn't have any post fields. 
2019-03-28 9:47:15.080107 - DEBUG --> GFAPI::send_notifications(): Gathering notifications for form_submission event for entry #11. 
2019-03-28 9:47:15.080271 - DEBUG --> GFCommon::send_notifications(): Processing notifications for form_submission event for entry #11: Array
(
    [0] => 5c4735e5ea004
)

(only active/applicable notifications are sent) 
2019-03-28 9:47:15.080456 - DEBUG --> GFCommon::send_notification(): Starting to process notification (#5c4735e5ea004 - Admin Notification). 
2019-03-28 9:47:15.092461 - DEBUG --> GFCommon::send_email(): Sending email via wp_mail(). 
2019-03-28 9:47:15.092652 - DEBUG --> Array
(
    [to] => terry@hyperweb.ca
    [subject] => SNA Quote Form Submission
    [message] => <html>
	<head>
		<title>SNA Quote Form Submission</title>
	</head>
	<body>
		<strong><em>Online Freight Quote Contact Information</em></strong><br />
<br />
<strong>First Name</strong> : Nikolay<br />
<strong>Last Name</strong> : Birimski<br />
<strong>Company Name</strong> : Hyperweb Test<br />
<br />
<strong>Address</strong> : <br />
<strong>City</strong> : Hyperweb Test<br />
<strong>State/ Province</strong> : Yukon<br />
<br />
<strong>Zip/ Postal Code</strong> : <br />
<strong>Telephone</strong> : 08888888<br />
<strong>Mobile</strong> : <br />
<br />
<strong>Email</strong> : nicky@hyperweb.ca<br />
<br />
<strong><em>Online Rate Quote Shipping Information</em></strong><br />
<br />
<em>Origin</em><br />
<strong>City</strong> : Hyperweb Test<br />
<strong>State/ Prov</strong> : Arkansas<br />
<strong>Business/Residential (Origin)</strong> : Business<br />
<strong>Checkbox (Origin)</strong> : <br />
<br />
<em>Destination</em><br />
<strong>City</strong> : Hyperweb Test<br />
<strong>State/ Prov</strong> : Alaska<br />
<strong>Business/Residential (Destination)</strong> : Business<br />
<strong>Checkbox (Destination)</strong> : <br />
<br />
==============================================================<br />
<br />
<strong>TL (Truckload) or LTL (Less than Truckload)?</strong> : Truckload<br />
<br />
<em>Truckload</em><br />
<strong>Service Required</strong> : Standard Service<br />
<strong>Equipment Required</strong> : 53' High Cube Dry Van<br />
<strong>On Pallets ?</strong> : Yes<br />
<strong>Total Weight</strong> : 1<br />
<strong>Lbs./ Kilos</strong> : Kilos<br />
<br />
<em>LTL</em><br />
<strong>Stackable ?</strong> : <br />
<strong># of Loose Items</strong> : <br />
<strong># of Loose Boxes</strong> : <br />
<strong># of Pallets</strong> : <br />
<strong># of Crates</strong> : <br />
<br />
<strong>All dimensions are in:</strong> : <br />
<br />
<strong>Dimensions &amp; Quantities of Loose Items &amp; Boxes, Pallets and Crates</strong> : <br />
<br />
<strong>Service Required</strong> : <br />
<strong>Equipment Required</strong> : <br />
<strong>Total Weight</strong> : <br />
<strong>Lbs./ Kilos</strong> : <br />
<strong>Class</strong> : <br />
<strong>Tot. Value</strong> : <br />
<br />
==============================================================<br />
<br />
<strong>Any Hazardous Material ?</strong> : No<br />
<strong>If Hazmat, Please Enter Class/U.N. #</strong> : <br />
<br />
<strong>Shipment Pickup Date</strong> : Actual<br />
<strong>Select a Shipping Date</strong> : 15/03/2019<br />
<br />
<strong>General Description of Goods/Special Instructions</strong> : Hyperweb Test<br />
<br />
<strong>Please Contact Me By</strong> :<br />
<strong>Newsletter</strong> : 
	</body>
</html>
    [headers] => Array
        (
            [From] => From: "Ship North America" <nicky@hyperweb.ca>
            [Bcc] => Bcc: terry@hyperweb.ca
            [Content-type] => Content-type: text/html; charset=UTF-8
        )

    [attachments] => Array
        (
        )

    [abort_email] => 
)
 
2019-03-28 9:47:15.156177 - DEBUG --> GFCommon::send_email(): Result from wp_mail(): 1 
2019-03-28 9:47:15.156322 - DEBUG --> GFCommon::send_email(): Mail was passed from WordPress to the mail server. 
2019-03-28 9:47:15.157927 - DEBUG --> GFFormDisplay::handle_confirmation(): Sending confirmation. 
2019-03-28 9:47:15.158998 - DEBUG --> GFFormDisplay::handle_confirmation(): Confirmation => Array
(
    [redirect] => 
)
 
2019-03-28 9:47:15.159130 - DEBUG --> GFFormDisplay::process_form(): Executing functions hooked to gform_after_submission. 
2019-03-28 9:47:35.959644 - DEBUG --> GFCommon::post_to_manager(): endpoint: https://www.gravityhelp.com/wp-content/plugins/gravitymanager/api.php?op=upgrade_message&key=0c226b1ae0ce43b4c7100e1f679c3ac1 
2019-03-28 9:47:36.179590 - DEBUG --> GFCommon::log_remote_response(): code: 200; body: <!--GFM-->

It appears that this line:
if ( false == $_POST['input_68'] ) return;

Should actually be this:
if ( false == $_POST['input_68_1'] ) return;

Could be input_68_3 too btw (they both have a value of 1, which equals true).

Actually, with the Consent field, it looks like it might be THIS!

if ( false == $_POST['input_2_68_1'] ) return;

I got mine from the ’ pre_submission_handler(): POST => Array’ output:

Yeah, weird huh? I have not worked with the consent field. I am curious if any of that is present when the form is submitted if the box is NOT checked.

Also, I am wondering if the revision of what you are consenting to is stored in that id?

May have to experiment…

© 2008 - 2021. Gravity Forms is a project by Rocketgenius Inc.