3

Gravity Forms offers a reasonably good documentation and of course Google offers more insights into how developers have solved other challenges related to this plugin - but we need to achieve what seems a relatively simple task, but cannot find any documented method to do it.

We are using the gform_after_submission hook to open a transaction to a SOAP web service to transmit some of the submitted form values.

The point at which we're stuck on is now to get the selected value ( or values ) from radio and checkboxes ( and also presumably <select>'s ).

Gravity Forms uses a system to pass the fields by ID and uses decimal points to pass values from radios - for example:

Field Name: Prefix - Field ID: 1 - Field Values: 1.1 "Mr.", 1.2 "Mrs."

Gravity Forms passes an array of the values to the hook - if the "Mr." option is selected these are:

array(..) {
    ["1.1"]=> string(0) "Mr."
    ["1.2"]=> string(0) ""
}

But the Array has no indication of which value was selected - it simply passes a string for both.

Not such a problem when only dealing with 2 values - but the fields may contain hundreds of values and these values can be updated by the client via the admin interface.

Searching the Gravity Forms codebase shows a few possibilities - such as:

$value         = RGFormsModel::get_lead_field_value( $lead, $field );
$display_value = GFCommon::get_lead_field_display( $field, $value, $lead['currency'] );

This requires the $lead object - which is also called the $entry object - this is available, but the method does not return the correct value, citing missing data

Another option is:

GFFormsModel::get_lead_field_value( $lead, $field );

However, this requires us to pass a correctly formatted $field object - which is part of the $form object - but again, not the cleanest route.

One idea is to search the $_POST object for the submitted values - this only includes the set values and not the empty ones - so in the case of Prefix:

["input_1_1"]=> string(3) "Mr."

A solid solution would rely on information about the field stored inside Gravity Forms, such as the number of values each field has - loop over them and check if each has a value set, before returning a formatted string with each selected value separated by a comma.

Perhaps Gravity Forms already has a pre-existing solution for this problem - however there is nothing in the documentation - any insights welcomed!

closed as off-topic by Mark Kaplun, TheDeadMedic, birgire, Howdy_McGee, Mayeenul Islam Nov 30 '15 at 7:10

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

8

If you have the form/field object you can retrieve a comma separated string containing the fields selected choices by using the GF_Field::get_value_export() method which was added in Gravity Forms 1.9.13.

If you are only going to use it with one or two fields you could do something like this:

$field_id    = 4;
$field       = GFFormsModel::get_field( $form, $field_id );
$field_value = is_object( $field ) ? $field->get_value_export( $entry ) : '';

The above would return the values for the selected choices, if you wanted to return the choice text you would set the third parameter of get_value_export() to true e.g.

$field_value = is_object( $field ) ? $field->get_value_export( $entry, $field_id, true ) : '';

If you need to access all the field values in the entry, but you want the relevant field types formatted to use the choice text then you could do something like this:

add_action( 'gform_after_submission', function ( $entry, $form ) {

    foreach ( $form['fields'] as $field ) {
        $field_value = $field->get_value_export( $entry, $field->id, true );
        // do something with the field value.
    }

}, 10, 2 );
  • This gives me all of the checkboxes, not just the ones that are checked and nothing distinguishes the checked from the unchecked. Based on what you were looking for, and you affirmation of the answer it seems like it should only show checkboxes, right? – Tiffany Israel May 12 '17 at 14:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.