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.

How do you add a PHP variable as a hidden input field?

  1. karlbitz
    Member

    I can't seem to find this anywhere and it seems like it should be pretty simple.

    All I want to do is populate a hidden input with a pre-existing PHP variable.

    For example - let's say I already know the user's email. When they fill out the form, normally I might do something like:

    <input type="hidden" id="email" value="<?php echo('$email'); ?>" />

    How would I replicate this behavior using a Gravity Form?

    Thanks!

    Posted 13 years ago on Wednesday March 30, 2011 | Permalink
  2. You would do this using custom PHP and the gform_field_value filter hook. Documentation for this hook is below:

    http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name

    In order to use this hook you must do the following:

    - Edit your Form
    - Add a hidden field you want to populate and give it a name
    - Edit the hidden field
    - Select the Advanced tab
    - Select the "Allow field to be populated dynamically" checkbox
    - Give the field a parameter name, this parameter name is used to target the field so name it something simple like "email" or something similar.
    - Save your form

    Then you would use the gform_field_value hook by adding custom code in your themes functions.php file to dynamically populate that field. For example, if you set the parameter to "email" your code would look like this:

    <?php
    add_filter("gform_field_value_email", "populate_email");
    function populate_email($value){
       return "email@value.com";
    }
    ?>
    Posted 13 years ago on Wednesday March 30, 2011 | Permalink