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 checkboxes

  1. schnettker
    Member

    Hi,

    i tried the following example:
    http://www.gravityhelp.com/forums/topic/dynamic-checkboxes (Posting 2)

    and it nearly works. My problem is that only the first value is returning. If i edit the field and put some empty "value rows" in, then there are more checkbox-values returning. But my problem is, that i don't know how much empty value rows i have to put in because i don't know how much values the database returns.

    Posted 7 years ago on Monday April 30, 2012 | Permalink
  2. David Peralty
    Administrator

    Can you paste your code here so I can take a look at it? And do you have a URL you can include to the example form you are trying to build?

    Posted 7 years ago on Monday April 30, 2012 | Permalink
  3. schnettker
    Member

    http://www.gwinnr.de/ess-gewinnspiel-melden/

    that is the ulr for the form

    the code:

    function ess_cat($form){
        foreach($form['fields'] as &$field){
            if(strpos($field['cssClass'], 'preise') === false)
                continue;
            $categories= get_categories(array('child_of'=>'15','hide_empty'=>0));
            $choices = array();
    		$i=0;
            foreach($categories as $category){
                $choices[$i]['text'] = $category->name;
    			$choices[$i]['value'] = $category->slug;
    			$i++;
            }
    
            $inputs = array();
    		$i=0;
            foreach($categories as $category){
                $inputs[$i]['label'] = $category->slug;
    			$inputs[$i]['id'] = "7".$i;
    			//7 ist die Field Id
    			$i++;
            }		
    
    		$field["inputs"] = $inputs;
            $field['choices'] = $choices;
        }
        return $form;
    }

    i have to extended the value part with many empty rows

    http://imageshack.us/f/85/2012043014h5755.jpg/

    Posted 7 years ago on Monday April 30, 2012 | Permalink
  4. David Peralty
    Administrator

    I don't think you should wrap everything you are doing in a foreach because each subsequent nested foreach will be run multiple times, thus creating your issue with the arrays of values you are then trying to pass back to the fields.

    Posted 7 years ago on Monday April 30, 2012 | Permalink
  5. You shouldn't have to add any checkbox items to your checkbox field in the form editor in order to dynamically populate it using the code example you are using. It replaces the values entirely, the existing values don't act as placeholders.

    I just tested the example you are working from and implemented it on my own test site and it works fine. The example I am referring to is here:

    http://www.gravityhelp.com/forums/topic/dynamic-checkboxes#post-9476

    I implemented this code, exactly as is, on my test site and had no problem using it to dynamically populate the checkbox field. I am including screenshots of what I have done below.

    I created a new form and on that form I placed a Checkbox field that I want to dynamically populate. The field consists of only 1 choice, but it doesn't matter as all the choices will be replaced by my custom code. See this screenshot:

    http://dl.dropbox.com/u/185347/Slingshot/Pictures/Screen%20Shot%202012-04-30%20at%2011.36.52%20AM.png

    After creating the form with the checkbox I then implemented the example code to populate that field dynamically using the gform_pre_render hook by placing custom code in my themes functions.php file. See this screenshot:

    http://dl.dropbox.com/u/185347/Slingshot/Pictures/Screen%20Shot%202012-04-30%20at%2011.37.48%20AM.png

    Now when I preview my form, the checkbox field is dynamically populated with the items it's being populated with in the custom code that I implemented as opposed to the choices in the form editor itself. See this screenshot:

    http://dl.dropbox.com/u/185347/Slingshot/Pictures/Screen%20Shot%202012-04-30%20at%2011.38.25%20AM.png

    So the example code does indeed work. If it's not working in your implementation that means that it wasn't implemented properly or the custom code related to getting and returning the values you want to populate the field is not functioning correctly.

    I would start again from the beginning and implement the code exactly as it is found here:

    http://www.gravityhelp.com/forums/topic/dynamic-checkboxes#post-9476

    Then update it to suit your needs. The issue is happening on your end with how you are populating the array of choices and inputs. It sounds like if it's only passing the first value, that you aren't actually passing an array of values like you should be.

    Posted 7 years ago on Monday April 30, 2012 | Permalink
  6. schnettker
    Member

    Hi Carl, my output is okay too. But please try to choice the second Checkbox and look then into your database. I tried several times an the second (third, and so on) checkbox isn't publish to database (the visiual output is okay)

    Posted 7 years ago on Monday April 30, 2012 | Permalink
  7. David Peralty
    Administrator

    So what is output to the database? When you do view source and look at the code being output for the list, what are the values associated with the choices?

    Posted 7 years ago on Monday April 30, 2012 | Permalink
  8. I do see what you are referring to. I am going to take a deeper look and see what is going on. I'll reply back shortly.

    Posted 7 years ago on Monday April 30, 2012 | Permalink
  9. schnettker
    Member

    Is there anything new?

    Posted 7 years ago on Monday May 7, 2012 | Permalink
  10. Ok, so using gform_pre_render to render the checkboxes is fine. But is there a way to set one or more boxes as CHECKED with this method? PLEASE HELP! :)

    Posted 7 years ago on Sunday August 26, 2012 | Permalink
  11. @sangfroidweb - please post the code you are using the render the checkboxes, and please post a link to your form and we will be happy to help you.

    Posted 7 years ago on Monday August 27, 2012 | Permalink
  12. ok has anyone figured this out yet...
    i am using the latest version and i have several checkboxes in my form and when form is submited it makes several new custom fields for check box values and also says just says array for some custom field values.
    really need help on this one for it is stopping my site launch.....

    Posted 6 years ago on Tuesday December 25, 2012 | Permalink
  13. ok has anyone figured this out yet or is there a fix for this
    i am using the latest version and i have several checkboxes in my form and when form is submited it makes several new custom fields for check box values and also says just says array for some custom field values.
    really need help on this one for it is stopping my site launch.....

    Posted 6 years ago on Tuesday December 25, 2012 | Permalink
  14. @nitestop, I will close this topic as a duplicate of your other post here:

    http://www.gravityhelp.com/forums/topic/gravity-checkbox-to-advanced-custom-fields-checkbox#post-107612

    Posted 6 years ago on Wednesday December 26, 2012 | Permalink

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