Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BronsonQuick/2834114 to your computer and use it in GitHub Desktop.
Save BronsonQuick/2834114 to your computer and use it in GitHub Desktop.
Populate a hidden field in Gravity Forms before submission
<?php
/* gform_pre_submission will do all forms. gform_pre_submission_1 will do a form with an ID of 1
* Keep an eye on the priority of the filter. In this case I used 9 because the Salesforce plugin we used ran a presubmission filter at 10 so we needed this to happen before it
*/
add_filter( "gform_pre_submission_1", "add_salesforce_campaign_id_footer", 9 );
function add_salesforce_campaign_id_footer( $form ){
foreach($form["fields"] as &$field)
if($field["id"] == 2){
/* Set the variable you want here - in some cases you might need a switch based on the page ID.
* $page_id = get_the_ID();
*/
$campaign_id = '701200000004SuO';
/* Do a View Source on the page with the Gravity Form and look for the name="" for the field you want */
$_POST["input_2"] = $campaign_id;
}
return $form;
}
?>
@mager19
Copy link

mager19 commented Mar 31, 2022

hi Bronson thanks so much, this code help to assing value to a hidden field before sum all values of checkboxes, look:

`
add_filter("gform_pre_submission_1", "obtainsumvaluesfield", 9);
function obtainsumvaluesfield($form)
{
// get all entries in this form
$entry = GFFormsModel::get_current_lead();

$all = 0;
foreach ($form["fields"] as &$field)
    if ($field->type == 'checkbox') {

        $field_value = $field->get_value_export($entry, $field->id);

        $values = explode(', ', $field_value);

        $all += array_sum($values);
    }

$_POST["input_21"] = $all;
return $form;

}

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment