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.

Populating a form Dynamically

  1. lukebream
    Member

    Hi

    I have a form which i want to use to edit a post that a user has created.

    I have the follwoing code and I need to pre-populate the example field in the gravity form in this case with "HELO" but obviously will eventually be the variable generated by the query. Im inside the wp-loop BUT i'm quirying a different post ID from the one that the loop is displaying.

    The gravity form is embeded from the post-dashboard using the shortcode

    This code is not prepolulating the form...? Any thoughts would be really appreciated.

    Thanks

    <?php  $targetPost = $_GET['postID']; 
    
     $postTitle = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM $wpdb->posts WHERE ID=$targetPost;"));
    
    echo '<p>Post Title is: ' . $postTitle . '</p>';
    
     add_filter("gform_field_value_form_post_title", "populate_form_post_title");
    
    function populate_form_post_title($value){
    
    	return "HELLO";
    
    }
    ?>
    Posted 12 years ago on Tuesday May 10, 2011 | Permalink
  2. Have you set the field you want to be populate so it can be populated dynamically? If so is "form_post_title" the parameter name you used to do so?

    - Edit the form
    - Edit the field
    - Select the advanced tab
    - Check the "allow field to be populated dynamically" checkbox
    - Give the field a parameter name, the parameter name is what is used with the gform_field_value hook. In your care "form_post_title" would be the parameter name.

    You only provided part of the information. If it's not populating it's because you either didn't set the field to be populated dynamically or you aren't properly implementing the code to populate the field. The hook itself should work if used properly.

    Posted 12 years ago on Tuesday May 10, 2011 | Permalink
  3. lukebream
    Member

    Hi Carl

    Thanks for coming back to me. I already had the settings done to allow form to be populated dynamivaclly.

    I tested by putting my code into the theme functions file and it populates the form field correctly.

    I have changed the way the form is embeded so that it is embeded from my page template and this is also where my other code is. So I have the following code located on a page template inside the LOOP....which is still not working

    BTW....I checked and the dynamic field name is correctly form_post_title

    So my code is:

    <?php  $targetPost = $_GET['postID']; ?>
    
    <?php echo $targetPost; ?>				
    
    <?php $postTitle = $wpdb->get_var($wpdb->prepare("SELECT post_title FROM $wpdb->posts WHERE ID=$targetPost;"));
    	echo '<p>Post Title is XXXXXX: ' . $postTitle . '</p>';
    ?>
    
    <?php gravity_form(3, false, false, false, '', true); ?>
    
    <?php add_filter("gform_field_value_form_post_title", "populate_form_post_title");
    	function populate_form_post_title($value){
    
    	return "helo";
    
    	//return get_post_meta($post->ID, 'property_feed_id', true);
    }
    ?>
    Posted 12 years ago on Tuesday May 10, 2011 | Permalink
  4. lukebream
    Member

    I have just been reading about add_filter and as I understand it I cannot use it on a page templat ...only inside the theme. funcions.php file.

    So hpw can I make this work from the page tem[plate ??

    Posted 12 years ago on Tuesday May 10, 2011 | Permalink
  5. So the problem is when you try to do this from the LOOP? If you try to do it on an individual page and place the code in your functions.php file it works?

    Posted 12 years ago on Tuesday May 10, 2011 | Permalink
  6. lukebream
    Member

    yes correct

    Posted 12 years ago on Tuesday May 10, 2011 | Permalink
  7. Hi Luke,

    Could you clarify why you need to add the filter in the loop as opposed to in the functions file?

    Posted 12 years ago on Tuesday May 10, 2011 | Permalink