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.

gform_pre_render upon previous dropdown condition?

  1. Hey guys, hope all is well at GF

    I have a question regarding gform_pre_render?

    I have dealer form. Which basically you choose your county, and then your dealer.

    Dropdown A = Dealer Country
    Dropdown B = Dealer Name

    I have about 15 countries, and I am using get_terms in the gform_pre_render to list all my countries that I have assigned to a dealer...

    // Dropdown A - Dealer Country
    add_filter("gform_pre_render", "dropdown_dealer_country");
    add_filter("gform_admin_pre_render", "dropdown_dealer_country");
    function dropdown_dealer_country($form){
    
        if($form["id"] != 3)
           return $form;
    
        $terms = get_terms("dealer-country");
    
        $items = array();
        $items[] = array( "text" => __('Select country...','mission-theme'), "value" => 0 );
    
        foreach($terms as $term)
            $items[] = array( "text" => $term->name, "value" => $term->slug );
    
        foreach($form["fields"] as &$field)
            if($field["id"] == 6){
                $field["choices"] = $items;
            }
    
        return $form;
    }

    OK so the above works perfectly in Dropdown A

    Now what I am trying to do in Dropdown B is display all my dealer names.

    I have named each dealer custom post-type title with the name of the dealer, and this is what I am pre populating my secondary drop down with...

    // Dropdown B - Dealer Name
    add_filter("gform_pre_render", "dropdown_dealer_name");
    add_filter("gform_admin_pre_render", "dropdown_dealer_name");
    function dropdown_dealer_name($form){
    
        if($form["id"] != 3)
           return $form;
    
        $dealers = get_posts(array(
        	"post_type" => "dealer",
        	"dealer-country" => $dealerCounty,
        	"post_status" => "publish",
        	"orderby" => "title",
        	"order" => "ASC",
        	"posts_per_page"  => -1
        ));
    
        $items = array();
        $items[] = array( "text" => __('Select dealer...''mission-theme'), "value" => 0 );
    
        foreach($dealers as $dealer)
            $items[] = array( "text" => $dealer->post_title, "value" => $dealer->post_name );
    
        foreach($form["fields"] as &$field)
            if($field["id"] == 7){
                $field["choices"] = $items;
            }
    
        return $form;
    }

    ...and as you can see in line 11, I have a variable in my get_posts array "dealer-country" => $dealerCounty

    My question, is it some how posible to get the choice that is made in Dropdown A into my $dealerCounty variable in my Dropdown B function?

    Any tips or help would be much appreciated as my Dropdown B is currently very long, and I need to filter it down by country.

    Many Thanks
    Josh

    Posted 11 years ago on Monday January 28, 2013 | Permalink
  2. David Peralty

    You would have to use JavaScript (AJAX) to do this, by capturing the choice and doing stuff to it and changing your second drop down.

    Posted 11 years ago on Monday January 28, 2013 | Permalink
  3. Great, I did just that, see code below I used. Thanks

    Upon change of Dropdown A I have an ajax function request tha re-populated Downdown B with the filtered options based on the selection in Dropdown A.

    See the ajax jquery script...

    [php]
    countryFilter = function () {
        	var countryClass = '.dealer-country select',
        		dealerClass  = '.dealer-name select';
        	$(countryClass).change(function(){
        		var countrySelect = $(this),
        			country = countrySelect.val(),
        	    	dealerSelect = countrySelect.parents('form').find(dealerClass);
        	 	if(country != "default") {
        	    	$.ajax({
        				type: 'POST',
        				url: '<?php echo admin_url('admin-ajax.php'); ?>',
        				data: { dealerCountry : country, action: 'get_dealer_name' },
        				success: function(data){
        					dealerSelect.empty();
        					var options = $.parseJSON(data);
        					for(i=0;i<options.length;i++){
        						dealerSelect.append('<option value="'+options[i].value+'">'+options[i].text+'</option>');
        					}
        					dealerSelect.removeAttr('disabled');
        				}
        	    	});
        	    }
        	});
        }

    Which I fired using...

    [js]
    $(document).ready(function () {
        	countryFilter();
        });
        $(document).bind('gform_post_render', function(event, form_id){
        	if(form_id == 3) {
            	countryFilter();
        	}
        });

    I then trimmed my original functions to...

    [php]
    // Dropdown A - Dealer Country
        add_filter("gform_pre_render", "dropdown_dealer_country");
        add_filter("gform_admin_pre_render", "dropdown_dealer_country");
        function dropdown_dealer_country($form){
                if($form["id"] != 3)
                   return $form;
                $terms = get_terms("dealer-country");
                $items = array();
                $items[] = array( "text" => __('Select country...','theme'), "value" => 'default' );
                foreach($terms as $term)
            		$items[] = array( "text" => $term->name, "value" => $term->slug );
                foreach($form["fields"] as &$field){
                    if($field["id"] == 6 ){
                        $field["cssClass"] = 'dealer-country';
                        $field["choices"] = $items;
                    }
            	}
                return $form;
            }
            // Dropdown B - Dealer Name
        add_filter("gform_pre_render", "dropdown_dealer_name");
        add_filter("gform_admin_pre_render", "dropdown_dealer_name");
        function dropdown_dealer_name($form){
                if($form["id"] != 3)
                   return $form;
                $items = array();
                $items[] = array( "text" => __('Select dealer...','theme'), "value" => 'default' );
                foreach($form["fields"] as &$field){
                    if($field["id"] == 7){
            			$field["cssClass"] = 'dealer-name';
            			$field["choices"] = $items;
                    }
            	}
                return $form;
            }

    Then the finishing touch is the function which also goes in the functions.php - this is called by the ajax request...

    [php]
    function get_dealer_name_fn(){
        	$dealerCountry = $_POST['dealerCountry'];
        	$dealers = get_posts(array(
                "post_type" => "dealer",
                "dealer-country" => $dealerCountry,
                "post_status" => "publish",
                "orderby" => "title",
                "order" => "ASC",
                "posts_per_page"  => -1
        	));
        	$items = array();
        	$items[] = array( "text" => __('Select dealer...','theme'), "value" => 'default' );
        	foreach($dealers as $dealer){
        		$items[] = array( "text" => $dealer->post_title, "value" => $dealer->post_title );
        	}
        	echo json_encode($items);
        	die;
        }
        add_action('wp_ajax_get_dealer_name', 'get_dealer_name_fn');
        add_action('wp_ajax_nopriv_get_dealer_name', 'get_dealer_name_fn');
    Posted 11 years ago on Friday February 1, 2013 | Permalink
  4. Very nice solution. Thank you for posting your code.

    Posted 11 years ago on Monday February 4, 2013 | Permalink

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