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.

depending fields dropdowns and checkboxes

  1. hittheroadjack
    Member

    Maybe, this one is easy.
    But somehow, I cannot find the proper information on this.
    I would like to offer a form to my customers where they can delete post attachments. For this, I have inserted a pre-populated dropdown field where the posts are listed the current user has author rights on. Now, depending on the selection in the dropdown, they should be offered a field of checkboxes with the attachements of the just selected post. This is before submission of the form.
    Is there a filter or hook to notify me when useres are changing selection in a pre-submission form? In fact, it is not even pre-submission because the user is not done making changes. Or do I have to scan the dropdown choices in the $Formobject for changes? What hook/filter could be used for that?

    Posted 6 years ago on Sunday November 11, 2012 | Permalink
  2. To be able to offer a list of attachments on a post, you will need to have all those attachments up front, which means knowing all the attachments for all the posts. Sounds like you're already retrieving all the posts for a specific author. In that function, however you're doing that, you can also retrieve all the attachments for all those posts.

    There is no hook to know when a user is changing a selection, i.e. a drop down in this case. You could use jQuery to watch for that, but I'm not sure that would help you.

    You would not scan the $form object since it does not change.

    I think as described above, to be able to show all attachments for a post, you need to know all attachments for all posts for that author, before the form is rendered, by retrieving the information and displaying it with gform_pre_render and conditional logic.

    Posted 6 years ago on Monday November 12, 2012 | Permalink
  3. hittheroadjack
    Member

    This sounds kind of insane with quite a few hundred posts.
    I think, I better consider a two-stage concept by letting the user select the post first, submit this selection and thereafter offer him a new 'deletion' form with pre-populated checkboxes for this post's specific attachments along with a security message 'Do you really want to do this?'.
    Is there an example of how to populate checkboxes using multiple choice and gform_pre_render?

    Posted 6 years ago on Saturday November 17, 2012 | Permalink
  4. Please see this topic http://www.gravityhelp.com/forums/topic/pre-rendered-checkboxes-wont-submit#post-74971 and the rest of the posts in that topic for an example of how to do it.

    Posted 6 years ago on Saturday November 17, 2012 | Permalink
  5. hittheroadjack
    Member

    Thanks Chris!
    I did it and works fine now!
    Having gained some time now, I would like to explore if I could make it with a jquery.ajax () request on the same form without submission.
    Here are my thoughts:
    - for the dropdown element I bind it to a .change() event
    - when changed, the event is launching a $.ajax -request calling a server-side php function
    - this php function is doing the pre-population in the $form object to ensure that the $form object is actual and then respond on the request
    - the ajax response is going to the checkbox fields of my form via 'success' method putting html straight to the checkbox elements
    Is this the right way it would work with ajax?
    And how can I address the form elements in jquery? Let's say I have a form id #25, a dropdown field #3 and checkboxes #1. Would it be something like$('.gform_id_25 .gform_field_id_3').change(...) or similar?

    Posted 6 years ago on Monday November 19, 2012 | Permalink
  6. hittheroadjack
    Member

    Need a little help on this!
    Meanwhile, I successfully wrote some routines using ajax and jQuery and with that, I am able to react on events like changing a GF dropdown field. Also, I found this one: http://www.gravityhelp.com/documentation/page/CSS_Targeting_Samples which helped a lot addressing GF fields.
    But now I am in doubt what has to happen next. When I 'override' GF fields using jquery - let's say checkboxes - do I need to modify the $form or $entry objects as well to get correct data on submission by the user? Or in other words: what is submitted with the form? The actual (modified) content of the HTML/CSS elements or the contents of the $entry/$form objects?

    Posted 6 years ago on Saturday November 24, 2012 | Permalink
  7. No matter how you make a selection or data entry in the form (be that jQuery to add the value or check the option, or a user actually making the entry) that is the value that will be submitted with the form. Has that been your experience? Can you share a link to your form so we can check it out?

    Posted 6 years ago on Monday November 26, 2012 | Permalink
  8. hittheroadjack
    Member

    EDIT: Sorry for posting early! I was wrong!
    If I address the elements correctly, I get the right values! I should have taken jQuery('#choice_16_2').val instead of jQuery('.gchoice_16_2').val. With #choice_16_2 everything is as expected and fine! Forget about the rest of the post!!

    Sorry but no! My current experience is different. Looking at the $entry object using a 'gform_after_submission' hook, I am getting 'wrong' values, i.e. the labels of checkboxes and their attached values are not submitting in the $entry object, although they have been changed by jQuery and displayed correctly in the form. Using Firebug, one can see the values and labels change in the HTML. On submission, I get the original label and value content as entered in the form editor ( equals the content of the $form object) . This is what I am doing:
    in the jQuery part:

    jQuery("#input_26_2").change(function() { //start function when dropdown changes
    	var sel=jQuery("#input_26_2 :selected").val();
    		jQuery.ajax({
    			type: "post",url: "/wp-admin/admin-ajax.php",dataType: "JSON", data: { action: 'randomcol2', _ajax_nonce: '<?php echo $nonce; ?>', avalue: sel},
    			beforeSend: function() {jQuery("#loading2").fadeIn('fast');}, //fadeIn loading just when changed
    			success: function(html){ //so, if data is retrieved, store it in html
    				jQuery("#loading2").fadeOut('fast');
    				jQuery("#colour4").val(html[0]['value']); //fadeIn the html inside helloworld div
    				jQuery("#input_26_15").val(html);
    //				var result = html.split('~');
    				var result = html;
    				jQuery('label[for="choice_16_1"]').html(result[0]['text']);
    				jQuery('.gchoice_16_1').val(result[0]['value']);
    				jQuery('label[for="choice_16_2"]').html(result[1]['text']);
    				jQuery('.gchoice_16_2').val(result[1]['value']);
    				jQuery('label[for="choice_16_3"]').html(result[2]['text']);
    				jQuery('.gchoice_16_3').val(result[2]['value']);
    				jQuery("#helloworld").fadeIn("fast"); //animation
    			},
    		}); //close jQuery.ajax

    and in the php part:

    function randomcol2() {
    	check_ajax_referer( "helloworld" );
    	$mapid = get_map_name($_REQUEST['avalue']);
    	$mapimg = get_images($mapid);
        foreach($mapimg as $image) {
                $image_deets = wp_get_attachment_image_src($image->ID);
                $choices[] = array('text' => "<div style=\"width:250 px; font-size:80%; text-align:center;\"><img src=\"{$image_deets[0]}\" alt=\"\" width=\"250\" height=\"163\" style=\"padding-bottom:0.5em;\"/><br/>".$image->post_title."</div>", 'value' => $image->ID);
            }
    	echo json_encode($choices);
    //    echo implode('~',$choices);
    	die();
    }
    add_action( 'wp_ajax_' . $_REQUEST['action'], $_REQUEST['action'], 1 );
    add_action( 'wp_ajax_nopriv_' . $_REQUEST['action'], $_REQUEST['action'], 1 );

    And how about your German? Here is your link: http://5.9.51.145/2012/noch-ein-test/
    When you click on the link with the tools icon you will get the form in a popup. Just change the dropdown selection to see the effect.
    Maybe, I should give a try to what Eugene Oprea said in this post: http://www.gravityhelp.com/forums/topic/pre-rendered-checkboxes-wont-submit#post-74971

    Posted 6 years ago on Monday November 26, 2012 | Permalink
  9. I was having a hard time following your train of thought. Do you have everything working as expected now?

    Posted 6 years ago on Tuesday November 27, 2012 | Permalink
  10. hittheroadjack
    Member

    Thanks Chris,
    everything is in perfect working order now.
    I have a couple of forms here where this method might be very helpful. If I only were better in programming, I would consider making an add-on plugin of it. If I can trust my search engines, this is a quite common problem for form designers and 'conditional logic' helps a lot but does not solve dynamic run time probs.
    Thanks again for helping!

    Posted 6 years ago on Tuesday November 27, 2012 | Permalink
  11. hittheroadjack
    Member

    Actually, I just started with writing an add-on plugin for GF using the approach from this thread.
    So far, I have a nice admin page to let admins define 'controller fields' and 'filter fields' where thfe 'filter fields' depend on events fired by action on 'controller fields'. In order to get this dependencies established, I need to bind them by jQuery either with the .live or the .on method. To avoid overloading, I thought I bind only those dependencies which are actually needed because their form is loaded on the DOM. I guess, I could retrieve this information somehow with jQuery, but is there also a hook/filter or GF Class method on the PHP side to find out which forms are actually present and equally important, which forms were unloaded- e.g. by closing page ??

    Posted 6 years ago on Thursday December 13, 2012 | Permalink
  12. hittheroadjack
    Member

    I have solved it now with jQuery and AJAX. My plugin is close to the last step before release now, just one little issue to overcome:
    in order to feed back the 'filtered' information via AJAX straigt to the DOM elements, I intended to use 'inside' functions of gravity forms like GFFormdisplay::get_field() or GFFormdisplay::get_field_content(). Thus I would be able to use a GF standard field object for filtering and feed back proper HTML code.
    Unfortunately, this has one hook: those functions use the constant IS_ADMIN for modifying some elements ids and classes of i.e. input fields. In case IS_ADMIN is TRUE you get "#input_3", if IS_ADMIN is FALSE you get "#input_20_3" - the first one without form-id the second one including it. The proper one in my case would be the latter one. But I can't get it returned by the function using an ajax request via admin-ajax.php because is_admin() is always set TRUE during handling of ajax requests by admin-ajax.php. For testing purposes, I have modified the definition of IS_ADMIN in gravityforms.php like this:

    define("IS_ADMIN", (is_admin() && !DOING_AJAX));

    This would solve the problem!
    Do I have a chance to get this modified in a next release of GF?
    Or is there any other workaround to get IS_ADMIN modified just for one function call to GFFormdisplay::get_field_content() or GFFormdisplay::get_field() during an ajax request?

    Posted 6 years ago on Saturday December 22, 2012 | Permalink
  13. Let me bring this to the attention of the development team and see what they say. Thank you for your hard work.

    Posted 6 years ago on Saturday December 22, 2012 | Permalink
  14. Your solution is interesting, but this is an area that we will be touching soon (to fix this IS_ADMIN problem once and for all), so I am a little hesitant to make this change as it could create more problems down the road. However, what I can do for you is add a filter to the IS_ADMIN so that you can implement it however you like. If that seems interesting, email me to alex@rocketgenius.com and we will get the ball rolling.

    Posted 6 years ago on Wednesday December 26, 2012 | Permalink
  15. ok is there a fix coming were we will be able to use multiple checkboxes and select fields...
    because they are not working on my site

    Posted 6 years ago on Sunday January 6, 2013 | Permalink
  16. There is no planned release date for that fix. If you would like assistance with your specific circumstances, please open a new topic and post the details there, including a link to the page on your site where the form is embedded. Thank you.

    Posted 6 years ago on Monday January 7, 2013 | Permalink

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