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.

Dynamically Populating Field with Variable through a PHP Embed

  1. stevebenjamins
    Member

    Hi, I just upgraded my license to developer and am finding more and more great uses for Gravity Forms every day. Thanks for doing great work :)

    I'm having some difficulty using a PHP variable to dynamically populate a field. When I use this code:

    add_filter("gform_field_value_jobname", "jobname");
    	function jobname($value){
             return "test";
    	}

    It dynamically populates the field perfectly. But when I try to dynamically populate with a variable:

    $name = $this->get_field('name');
    echo $name;
    	add_filter("gform_field_value_jobname", "jobname");
    	function jobname($value){
    	return $name;
    	}

    It doesn't work. I know $value is set, I just can't seem to figure out how to get $value dynamically populated!

    Any thoughts?

    Posted 12 years ago on Thursday August 11, 2011 | Permalink
  2. Shouldn't $name be inside your function?

    Posted 12 years ago on Thursday August 11, 2011 | Permalink
  3. stevebenjamins
    Member

    Ideally yes, but $name in this case is drawing from an outside database:

    $name = $this->get_field('name');

    And I can't use the database object from within the function :)

    Posted 12 years ago on Thursday August 11, 2011 | Permalink
  4. Hi Steve,

    Assuming that your "echo $name;" is working and the $name variable is correctly being retrieved from the outside database, declaring the $name as a global inside the jobname() function should allow you to access that variable. Currently, the $name variable is not within the scope of the jobname() function.

    [php]
    function jobname($value){
        global $name;
        return $name;
    }
    Posted 12 years ago on Saturday August 13, 2011 | Permalink