0

I am working on a project using gravity forms. I created a dropdown field(field id1), with labels 'option1, option2, etc' and gave them values (option1 = value 100, option2 = value 200)

I use the entry in a functions, let say I use the following:

 $example = $_POST["input_1"]    

in this case $example gives me the value of the choice (100 or 200). I want that sometimes, but I need it also to give me the field name (option1, or option2).

So what I want is something like:

 $example 2 = .........     

which makes $example2 to give me the field name.

is that possible?

1 Answer 1

1

I assume you use some hook, provided by Gravity Forms, to run your code. An appropriate one would be gform_after_submission, which provides your callback both with the form object and the entry object. The form object contains all the fields of the submitting form, with all the field names, the entry object contains the submitted values.

You can loop through the form fields with a simple foreach.

foreach( $form['fields'] as $key => $field ) {
    ...
}

The field object that we retrieve above, contains all the settings of the form fields, so you can find your particular field by checking for one of them, e.g. id or label. Because that field is a dropdown field, it contains even a choices array, with the label, the value and a default flag. You retrieve the label of a particular choice like this:

$label = $field['choices'][0]['text'];

The last thing to do is to read the entry object to get the submitted selected choice. Since you know the $field['id'], you simply get the submitted value like this:

$submitted_value = $entry[ $field['id'] ];

With the submitted value of the field you can check which of the choices in the field array has been selected and get both text and value of this choice.

Sign up to request clarification or add additional context in comments.

1 Comment

This technique doesn't always work. What if you have a dropdown that has the same value against multiple text fields? For example, you could have countries in a list and the value might be the currency. If you had England, Wales, Scotland, Northern Ireland, as four countries, they would have GBP as the currency. If you try to match the currency against text label, you have no idea what the person selected because in this example it could be one of four choices. The Gravity Forms $entry array is incomplete. It's a poor oversight by the developers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.