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.

Dynamic population - parameter outputs outside the form, but not in form fields

  1. Ketiga
    Member

    Hi,

    I have the following function in my theme function.php file:
    ---------------------------------------------------------------

    [php]
    if(isset($_POST['input_13'])){
    $my_value=$_POST['input_13'];
    }
    
    add_filter('gform_field_value_select_stuff', 'select_stuff');
    function select_stuff($value){
    
    global $my_value;
    
    $result=mysql_query("SELECT user_email
    FROM wp_users
    LEFT JOIN wp_usermeta
    ON wp_users.ID=wp_usermeta.user_id
    WHERE wp_usermeta.meta_key='$my_value'
    ");
    
    while($row = mysql_fetch_array($result)){
    	       return $row['user_email'];
       }
    if(mysql_num_rows($result)==0){
    return "No Match";
    }
    }

    ---------------------------------------------------------------
    I have two questions:

    1. the code works as expected when i put this in a page : <?php echo select_stuff() ?>

    But when I use the parameter name 'select_stuff' (without the '') in a gravity form field in the 'parameter name' field to dynamically populate the field, it shows the exception: "No Match". It's like my $my_value all of a sudden is not remembered when using the parameter method in a form field. I know this because when I use a static value instead of $my_value it DOES show the result inside the gravity forms field.

    2. My second issue is that 'return' in this function only outputs 1 value, while using echo or print shows all available matches (and there are multiple).

    Can anybody help me out with these two problems?

    Thanks in advance!

    Posted 11 years ago on Monday July 2, 2012 | Permalink
  2. Ketiga
    Member

    I simplified my function to make the debugging easier:

    add_filter('gform_field_value_select_stuff', 'select_stuff');
    function select_stuff(){
    
    if (isset($_POST['input_13'])) {
    return $_POST['input_13'];
    }
    }

    Still no luck. The result is shown just fine when used outside gravity forms but not when using a parameter_name to dynamically populate a gravity forms field.

    When I use echo instead of return in the function, the result shows outside the field above the form though, so I know the value is stored.

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  3. Can you explain what it is that you're trying to do? Maybe we can find a simpler way. Thanks.

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  4. Ketiga
    Member

    Hi,

    I am trying to compare a value in my database to a value entered in a gravity form field and when there's a match I want to be able to put results from my database in a gravity forms field (in my case I need to select e-mail addresses).

    In:
    WHERE wp_usermeta.meta_key='$my_value'

    $my_value what's entered in a gravity forms field value so I guess it could be this as well:

    WHERE wp_usermeta.meta_key=$_POST["input_13"]

    I then use the parameter name 'select_stuff' to try and dynamically populate my form field with the result of the function.

    But in Gravity Forms it won't output the result in a field. When I execute the function anywhere else in the page (outside gravity forms) or use a custom html form input field, it does show the result just fine.

    I just tested again with this simple code in my functions.php file.

    add_filter('gform_field_value_select_stuff', 'select_stuff');
    function select_stuff(){
    return $_POST["input_13"];
    }

    Works anywhere on my page except when I use parameter name 'select_stuff' in a Gravity forms field.

    The result of this part "No Match":

    if(mysql_num_rows($result)==0){
    return "No Match";
    }

    DOES show in my gravity form field with the parameter set, even though the previous condition is met...

    Hope it's somewhat clear..:)

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  5. David Peralty

    Your function should be:

    function select_stuff($value){

    Also, where are you getting input_13? A previous form?

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  6. Ketiga
    Member

    You are right, tried this before as well but it didn't work.

    input_13 is a field in the same form.
    The value entered in that field is used to compare with database values.
    There is a result when there's a match and this result should be returned in a separate field.

    The code works used as regular php. But in gravity forms "No Match" is what always shows in the result field, because somehow it looks like it loses the variable entered in 'input_13'

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  7. David Peralty

    So you are trying to have a field on a form that someone types into, and then that field is to check the database and then another field is supposed to get populated with a value?

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  8. Ketiga
    Member

    Exactly, I wish I could have been that clear from the start :D

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  9. David Peralty

    gform_field_value_ fires on loading of the field. So what is currently happening is that it is checking input_13 on load of select_stuff and that is before a user has a chance to put anything in. If input_13 had a default value of "Start" then select_stuff should have a value of Start. But when a user changes the text, it won't update what is in the select_stuff field because there is no JavaScript making this happen live.

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  10. David Peralty

    You need an action between a user typing into field one and the result showing in field two. So that the database can run its query and return a result from what a user typed in. Or you need some JavaScript (not my specialty) to do this using JQuery or something..

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  11. Ketiga
    Member

    Thank you for the explanation.

    What I don't understand is that simple html input fields show the result just fine. Isn't there a different gravity forms type of field or function that can be used for this?

    Also, the second field is on page two of my form so I expected it to load select_stuff again and then use the value of input_13 that was typed in. But I understand this is not the way it works either then?

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  12. David Peralty

    You could look into the following hook that fires during page changes:
    http://www.gravityhelp.com/documentation/page/Gform_post_paging

    Then you could grab the value from the previous page, do your query, and push it to the form field in the second page.

    Make sure the field has a check in allow to populate dynamically. And look at the documentation for pre_rendering a form as an example of the function you will want to create using the post_paging hook.

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

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink
  13. Ketiga
    Member

    I will take a look and let you know the results.

    Thanks for the help so far!

    Posted 11 years ago on Wednesday July 4, 2012 | Permalink