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.

Populating a hidden field based on dropdown selection

  1. thepresident
    Member

    Hello,

    I have created a dropdown, and I want to pass the value that's selected to a hidden field when it's submitted.

    Basically the form is used for submitted an advert, the dropdown is the number of images they require in the ad (options are 1-5), and the more images they submit, the higher they need to be in the listings. The dropdown is also a pricing field, which passes the value to the total. I just need to pass the dropdowns value to a hidden custom field, which is used to order the posts on the output page.

    Can anyone offer some advice on what I need to do?

    Thanks!

    Posted 10 years ago on Monday July 25, 2011 | Permalink
  2. You would have to do this with custom code you would add to your themes functions.php file to customize what that form does when it is submitted. You would use the gform_post_submission hook to take the value of the drop down field and update the hidden field with that value.

    The gform_post_submission hook is documented here:

    http://www.gravityhelp.com/documentation/page/Gform_post_submission

    Posted 10 years ago on Monday July 25, 2011 | Permalink
  3. thepresident
    Member

    Hi Carl

    Thanks for getting back to me. Unfortunately I'm pretty new to php so I am trying to work though the example you gave.

    I have amended the example to select the specific form, how to I format it so that I take the value from the dropdown and populate it into the hidden custom field? Is "Blender Version:" in the example the source (dropdown in my case)?

    Thanks

    Posted 10 years ago on Tuesday July 26, 2011 | Permalink
  4. You would access and manipulate the Entry objet via the gform_post_submission. The fields would be accessed by their ID in the code, not by their name. Each field has a Field ID, which you can get by viewing the source of the form and seeing the ID. The Entry object is documented here:

    http://www.gravityhelp.com/documentation/page/Entry_Object

    I would suggest searching around the forum for information on things like populating custom fields. Here is one thread I found that has example code:

    http://www.gravityhelp.com/forums/topic/map-form-field-to-custom-field-located-in-an-array

    When the $entry object is accessed the number being used is the field id.

    Posted 10 years ago on Tuesday July 26, 2011 | Permalink
  5. thepresident
    Member

    Thanks Carl.

    So rather than using a hidden field, I just need to send the value of the dropdown to the required custom field?
    I've changed the values of the example you directed me to, but haven't managed to get it to work. It looks like this so far:

    add_action("gform_post_submission", "add_custom_field", 10, 2);
    function add_custom_field($entry, $form){
        //NOTE: replace 1 with the ID of your form.
        if($form["id"] != 9)
           return;
    
        //getting post meta
        $meta = get_post_meta($entry["post_id"], 'key', true);
    
        //updating price property of array.
        //NOTE: replace "1" with the ID of the field you want to populate the price field.
        $meta["ranking"] = $entry["30"];
    
        //updating meta
        update_post_meta($entry["post_id"], "key", $meta);
    
    }

    Thanks, sorry PHP isn't my strong point!

    Posted 10 years ago on Thursday July 28, 2011 | Permalink