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.

Allow HTML Block to be dynamically populated

  1. mrhoneyfoot
    Member

    My goal is to show a different image inside a html block based on cross-checking two multiple choice fields. Let's say they're called style and colour. Is it possible to dynamically populate a html block field to achieve this?

    My only other solution is to create a different html block with it's own image for every possible combination of style/colour (hundreds) and show/hide them where necessary using conditional logic. It will probably work but feels a bit clunky from a back-end point of view. Is there a smarter way?

    Out.

    Posted 13 years ago on Saturday January 29, 2011 | Permalink
  2. Yes, you can use the Pre-render hook to dynamically change the HTML block content.
    Here is a code snippet to point you in the right direction. Place it in your theme's function.php file.

    add_filter("gform_pre_render", "populate_dropdown");
    
    function populate_dropdown($form){
    
        //only applies to form id 124. Replace 124 with your actual form id
        if($form["id"] != 124)
           return $form;
    
        //Modifying field id 1. Replace 1 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 1){
                $field["content"] = "My HTML block content";
            }
    
        return $form;
    }
    Posted 13 years ago on Saturday January 29, 2011 | Permalink
  3. mrhoneyfoot
    Member

    Thanks to Alex Cancado for the snippet. As I understand it, the code isolates the specific form ID and then field ID. Anyone got any idea about the next step? How you set out the rules for dynamically changing the content based on other form field selections, in other words?

    Take the examples in the explanation above for colour and style. If there are 10 colours and 20 styles, that's 200 combinations that we want the HTML block to display.

    Anyone know what you should use to achieve this, and how?

    Posted 13 years ago on Monday January 31, 2011 | Permalink
  4. mrhoneyfoot
    Member

    Quick question for you Alex. I don't understand how your snippet can change the content dynamically. So how does it work?

    Posted 13 years ago on Tuesday February 1, 2011 | Permalink
  5. mrhoneyfoot
    Member

    Just got this snippet, does it contain the answer?

    style = $('#style').val();
    color = $('#color').val();
    
    $.ajax({
         type: "POST",
         url: "get_image.php"
         data: { "style" : style, "color" : color },
         success: function(msg) {
              $("#image_field").html('<img src="' + msg + '" />');
         }
    });

    and in get_image.php

    $color = $_POST['color'];
    $style = $_POST['style'];
    var $image;
    
    // Logic goes here, setting $image to the appropriate image link
    
    echo $image;

    Anyone know how this fits into the snippet posted by Alex Cancado above?

    Thanks.

    Posted 13 years ago on Tuesday February 1, 2011 | Permalink
  6. Hi MrHoneyfoot,

    The level of customization you're attempting here is outside of the scope of standard support. We're happy to leave the topic open for fellow users to assist.

    Posted 13 years ago on Tuesday February 1, 2011 | Permalink
  7. mrhoneyfoot
    Member

    If I buy a developer license, is this kind of thing covered in the priority support?

    Posted 13 years ago on Tuesday February 1, 2011 | Permalink