Horje
gravityform use dropdown choice as variable in gform_pre_render - Wordpress Solution
[Solved] - gravityform use dropdown choice as variable in gform_pre_render - Wordpress Solution
Try following codes
gravity forms : use previous dropdown choice as variable in gform_pre_render? This question is mainly regarding [[LINK href="http://www.gravityhelp.com/documentation/page/Gform_pre_render"]]gform_pre_render[[/LINK]] which I am using in my functions.php pre-render 2 dropdowns, but applying some conditional logic manually to Dropdown B. In a nutshell, I have dealer form. Which basically you choose your county, and then your dealer. Dropdown A = Dealer Country Dropdown B = Dealer Name // this will be hidden by conditional logic until Dropdown A is chosen. I have about 15 countries, and I am using get_terms in this first gform_pre_render function to list all my countries that I have assigned to a dealer post, quite straight forward...

// 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 function works perfectly in Dropdown A. Now what I am doing in Dropdown B is displaying all my dealer names (post titles) from my dealer post-type. I have named each dealer custom post title with the name of the dealer, and this is what I am pre-populating Dropdown B 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

Solution - 1

Hi Josh, If only GravityForm has options to set class for each choice (option)... but we can re-populate all options for dealer name with ajax based on selected county try this on functions.php, [[LINK href="http://pastebin.com/vaeubLgz"]]http://pastebin.com/vaeubLgz[[/LINK]] and ajax for getting post name/title based on county: [[LINK href="http://pastebin.com/JTkmTrUV"]]http://pastebin.com/JTkmTrUV[[/LINK]] we need to add class on gform_pre_render for our javascript function to both county and dealer name field, for example on above code I use 'dealer-county' for county and 'dealer-name' for dealer name; from the documentation add "cssClass" property to the field ( $field["cssClass"] )

// 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["cssClass"] = 'dealer-county';
            $field["choices"] = $items;
        }
	}
    return $form;
}
and for dealer name, I think we don't need specific $dealerCounty category here, so we can populate all or none (since this field will be disable before county selected);
// 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",
    	"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["cssClass"] = 'dealer-name';
            $field["choices"] = $items;
        }
	}
    return $form;
}


Solution - 2

Are you looking to have this done without reloading the page? (i.e. ajax) Or are you breaking it up into two pages


Solution - 3

Josh, Maybe an idea is reload only content of Dropdown B with an AJAX call as Kyle says. But this would not be my idea. I would do my own form in this case and I will use AJAX easier. 1) Create a template with forms for this case 2) Create a page with an slug to be called 3) Include an AJAX call to fill it:


<script type="text/javascript" >
function populate_select()
{
$(function(){
$('#country2').change(function(){
var $tmp_Country2=$('#country2').val();
alert($tmp_Country2);
$("#city").empty();
$.ajax({
url:"/ROOT_TO_PHP_THAT_GENERATE_NEW_SELECT.php",
type:'POST', data:'country=' + $tmp_Country2,
success:function(results)
{
$("#city").removeAttr("disabled");
$("#city").append(results);
}
});
}
);
});
}
</script>

Date: 2022-10-21 05:03:27
Category: Wordpress
More for You