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.

Pre-Render and Single Line Text / Name

  1. I'm trying to pre-render info into a single line text input with a field['id'] of 2 - I can pre-render the label and description just fine, but I can't get my variable to show up in the single line input text box!
    I've tried "value", "input".. what am I missing?

    // Loop through the fields and look for the single line text with field id of "2"
    foreach($form["fields"] as &$field)
         if($field["id"] == 2)
         {
            $field["label"] = "This is the Label";                // Pre-Render the field label
    	$field["value"] = "This is the value";                // Pre-Render the field value
    	$field["description"] = "This is the Description";    // Pre-Render the field description
         }
    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  2. The gform_pre_render filter is designed to modify the form object, not the entry object. To pre-populate a value in your form using PHP, you can use the gform_field_value_$parameter_name filter http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name

    You need to enable the field to be populated dynamically (on the advanced tab for your form field, here field 2) then use that parameter name in your function, like this. So, if you used a parameter name of "college", you would use the function like this:

    [php]
    add_filter('gform_field_value_college', 'set_college');
    function set_college($value){
       return "Vassar";
    }

    Does that help?

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  3. Chris,

    Thanks for the explanation, but..

    I'm pulling data out of Form 1 to pre-populate fields in Form 2 using custom MySQL queries to the wp_rg_lead_detail table utilizing $_POST['form_id'] and $_POST['lead_id'] passed through the URL to get the info I need.

    I see how your explanation works to get hard-coded data to pre-render, but would you still suggest this strategy of adding filters and do the my_sql query with-in the filter functions for each field I need to populate in form 2?

    Thanks!

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  4. You can still use the same function. Instead of trying to do it with gform_pre_render, you need to use gform_field_value_$parameter_name. I don't see how it's any different than what you were doing before, other than this being the correct filter for populating a form field.

    Can you point out something I am missing in your process? If you can post more of your code at pastebin.com or pastie.org maybe we can come up with an elegant solution for you.

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  5. Hi Chris,

    Thanks for the prompt reply.

    I can populate a single line text input on my form with a hardcoded message - no problem.

    But what I need to do is a database lookup to get some form inputs out of Form 1, then populate some form fields in Form 2.

    Here is my code, but this isn't populating anything when I return $my_field. Can you point out anything I'm doing wrong?

    Thanks a ton!

    Code:
    http://pastebin.com/dAyDtHFm

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  6. I think on line 13 the 1.3 should be quoted, since it's a string. Like this:

    [php]
    if($form_data["field_number"] == '1.3')

    Also, be sure the field you want to populate is marked "allow field to be populated dynamically" and that this is the parameter name: 'singlelinetext' (without the quotes.)

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  7. Chris,

    Thanks for the suggestion - 1.3 was changed to '1.3', and the checkbox for "allow field to be populated dynamically" is checked and the parameter name singlelinetext is used (w no quotes).. but it didn't make any difference. Do you see anything else in the code that is wonky?

    Thanks!

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  8. Also on line 13, I'm not sure you're iterating over the array correctly. Since you are doing foreach($form_data["fields"] as &$field), you would normally check each $field, not each $form_data['whatever']. I would be curious to see what $form_data looks like on line 10 (with var_dump or print_r). That might give us a clue to the correct way to get at the values. You could also echo the value of $form_data["field_number"] around line 12.5 (between line 12, and 13, outside the loop.)

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  9. I think you're right Chris, but I can't seem to fix it.. I added a couple of counter variables that increment as I go through the iteration, and it appears $field["field_number"] has no values..

    Here is the var_dump($form_data):
    http://pastebin.com/GJuGcG77

    And here is a link to the code that contains the filter to populate the singlelinetext input box on one of my forms (and the var_dump above):
    http://pastebin.com/MMguScMU

    When I var_dump or print_r, I see that I have the correct record I'm looking for, but how the heck do i access the data once I have the right record??

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  10. It's hard to guess without having the code, the data and the form, but I think we're getting closer.

    Can you change line 11 in your last example FROM this:

    foreach($form_data["fields"] as &$field)

    to this:

    foreach($form_data as &$field)

    I'm not sure why "fields" was there.

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  11. Chris,

    I tried as you suggested, but changing it to
    'foreach($form_data as &$field)'
    breaks the script.

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  12. What is the error which shows up?

    I don't think I can help any more from here. Is this a live site or can I log in and take a look at the code you have and the data and form that you have? If I can log in to this site, please send me at WordPress administrator login to chris@rocketgenius.com - thank you.

    Posted 11 years ago on Wednesday December 26, 2012 | Permalink
  13. Hi Chris,

    I wound starting over with this and now it works like a top. I'll include the code so other people can use the the solution I worked out last night.

    As a summary, I'm using the gform_field_value_$parameter_name filter as you suggested. The documentation is here:
    http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name

    I'm doing custom MySQL queries to the wp_rg_lead_detail table to get data out of a form that's been previously submitted (Form 1), then populating that data into a new form (Form 2).

    The following code pulls the cell phone number out of Form 1, and dynamically populated the cell phone input box in Form 2.
    Key things to remember:
    1) you must change the following code to reflect your form number.
    2) you must enable the "Allow field to be populated dynamically" under the Admin tab on the form input you want to populate.
    3) you must use "cellphone" (with out quotes) as the dynamic population parameter.

    <?php
    
    	add_filter("gform_field_value_cellphone", "populate_cellphone");
    	function populate_cellphone($value) {
    		global $wpdb;
    		// Change this to be the number of the form you're using
    		$form_id = 1;
    		// Do a direct MySQL lookup based on the entry_id you need
    		$sql = "
    			SELECT
    				ld.lead_id,
    				ld.field_number,
    				ld.value
    			FROM
    				wp_rg_lead_detail ld
    				INNER JOIN wp_rg_lead l ON ld.lead_id = l.id
    			WHERE
    				ld.form_id = %d AND lead_id = '".$_REQUEST['entry_id']."'
    			ORDER BY
    				ld.lead_id,
    				ld.field_number
    		";
    		$sql = $wpdb->prepare($sql,$form_id);
    		$results = $wpdb->get_results($sql, ARRAY_A);
    		$form_answers = array();
    		foreach($results as $row) {
    			$row = (object)$row;
    			$major_num = (int)$row->field_number;
    			$minor_num = (int)(($row->field_number-$major_num)*10);
    			$form_answers[$row->lead_id][$major_num][$minor_num] = $row->value;
    		}
    		// Iterate through the array of data pulled from the dB
    		foreach($form_answers as $i => $lead) {
    			foreach($lead as $j => $form_answer) {
    				$form_answers[$i][$j] = implode(' ',$form_answer);
    			}
    		}
    		// DUMP the variables to see what's in the array
    		// Uncomment the line containing print_r below to see everything
    		// that's pulled from the MySQL Query. This will show you the correct
    		// position for the data you're looking for
    		//echo '<pre>'; print_r($form_answers); echo '</pre>';
    
    		$my_lead_id = $_REQUEST['entry_id'];
    
    		// Return the requested variable - note, 14 is the position of the
    		// cell phone number in the array of data pulled from the dB
    		return $form_answers[$my_lead_id]['14'];
    	}
    ?>
    Posted 11 years ago on Thursday December 27, 2012 | Permalink
  14. Thank you for posting your solution.

    Posted 11 years ago on Thursday December 27, 2012 | Permalink

This topic has been resolved and has been closed to new replies.