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 populate form with short code exec

  1. jimlongo
    Member

    Hi, I just purchased Gravity Forms and want to populate a field with some data from a query string. I use Shortcode-exec php to get the data I want. I can print it I know that it's there.

    I put that shortcode in the Parameter Name box and check that the field can be populated dynamically.

    I'm not clear what kind of field it should be. But I can't get any data to show up in that field.

    Any advice on how to proceed.

    Thanks,
    jim

    Posted 11 years ago on Tuesday March 19, 2013 | Permalink
  2. To use the query string, you need to send the parameters in the URL, like this:
    http://gravity.chrishajer.com/family-reunion-rsvp/?st=Illinois,Indiana,Nevada,Alabama,Confusion

    I have a field set up for dynamic population (my list of states) and a parameter name of 'st'. Then, I can send the individual states in the query string as shown above. There is no need for the shortcode-exec plugin and I'm not sure how that comes into play.

    Please explain more what you're trying to do. If you're trying to use the shortcode-exec plugin to populate a field in your form, it's possible you aren't using the query string at all.

    Posted 11 years ago on Tuesday March 19, 2013 | Permalink
  3. jimlongo
    Member

    Thanks Chris.

    I have some data that refers to a photo. It is currently in the URL in the form of xyz.com/?code=12345A (1235A refers to a file on a CDN).

    Both this filename and the url to that file are contained in the $_SESSION array as ['images']['filename'] and ['images']['url']

    In both cases I've previously been using shortcode-exec PHP to (as a way to use snippets of php in wordpress posts) to display these items when I need them. I'm open to any way of grabbing them for the purpose of using them in my form.

    I'd like to get those items into 2 fields. "Filename" and "URL".

    Posted 11 years ago on Tuesday March 19, 2013 | Permalink
  4. jimlongo
    Member

    Chris, I guess when you say shortcode you are talking about a gform shortcode. If that's right, I guess a hook would be more appropriate.

    I'm trying to get an example to work.
    I'm using the hard coded date example in a date field. I've put the following in my theme>functions.php file.

    add_filter("gform_field_value_date", "populate_date");
    function populate_date($value){
       return "10/10/2010";
    }

    I've checked the dynamic checkbox and entered "date" as the parameter.
    The field is still empty. What else do I need to know.
    I tried populate_date as the parameter as well as populate_date($value) but no result yet.

    I'm thinking if I get that to work, then I can use the gform_field_input or gform_field_content hook to populate the field with my data?

    Thanks,
    jim

    Posted 11 years ago on Tuesday March 19, 2013 | Permalink
  5. jimlongo
    Member

    add_filter("gform_field_input", "filename_in", 10, 5);
    function filename_in($input, $field, $value, $lead_id, $form_id){
    
    	$code = substr($_SERVER['QUERY_STRING'],5,12);
    	$filename = str_replace(array('-0', '-1'),array('-A', '-B'), $code);
    
    	if ($form_id == 1 && $field["id"] == 1){
    		$value = $filename;
    	}
    
    	return $value;
    
    }

    and using input as the dynamic parameter in the form?

    i can print $filename it is 12345-A, I want to populate the field with that data.

    Thanks,
    jim

    Posted 11 years ago on Tuesday March 19, 2013 | Permalink
  6. jimlongo
    Member

    Has something to do with where the hooks are added in functions.php

    Sometimes I get nothing.
    Sometimes I get results, but the form fields are gong, the results are printed on the page!?

    Posted 11 years ago on Tuesday March 19, 2013 | Permalink
  7. jimlongo
    Member

    I've tried adding just the gform_field_input in nearly every conceivable spot in the functions.php file.

    It does one of two things.

    1. Nothing
    2. It prints the result i want, but the form disappears and it just prints it onscreen.

    I just put the filter and function in my custom plugin file, and I get result #2 above.

    Please, What is wrong with my filter action?

    Posted 11 years ago on Tuesday March 19, 2013 | Permalink
  8. gform_field_input is used to change the field input tag in the HTML source of the form when the form is rendered. From reading your comments, I don't think that's what you want to do? I think I would use gform_field_value_photo (or whatever your field is called where you want to store the filename.)

    Are you trying to display the image in your form, or trying to store the image filename in a form field?

    Posted 11 years ago on Thursday March 21, 2013 | Permalink
  9. jimlongo
    Member

    Thanks Chris, I'm trying to store the image filename. I see what you mean value_foo will display the results of the function where foo is the parameter in the form setup.

    Eventually I will be interested in displaying the photo, if you could briefly explain that as well that would be appreciated.

    Thanks,
    jim

    Posted 11 years ago on Friday March 22, 2013 | Permalink
  10. Please try this code instead:

    [php]
    add_filter("gform_field_value_filename", "filename_in");
    function filename_in($value){
    
    	// only process this code for form ID 1
    	if ($form['id'] != 1)
    		return;
    
    	$code = substr($_SERVER['QUERY_STRING'],5,12);
    	$filename = str_replace(array('-0', '-1'),array('-A', '-B'), $code);
    	$value = $filename;
    	return $value;
    
    }

    Assuming your function worked to extract the filename from the QUERY_STRING, this will run your 'filename_in' function and will populate a field in your form which has been checked "allow field to be populated dynamically" (on the advanced tab for that field) and was given a parameter name of 'filename'.

    Posted 11 years ago on Sunday March 24, 2013 | Permalink