Skip to content

Instantly share code, notes, and snippets.

@c-nefi
Forked from joshmoto/gist:4663253
Last active December 11, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c-nefi/4663590 to your computer and use it in GitHub Desktop.
Save c-nefi/4663590 to your computer and use it in GitHub Desktop.
<!-- THE SCRIPT -->
<?php echo do_shortcode('[gravityform id="3" name="Competition Entry" ajax="true"]'); ?>
<script type="text/javascript">
jQuery(document).ready(function($) {
var countryClass = '.dealer-country select',
dealerClass = '.dealer-name select';
// disable dealer name before county county selected
$(dealerClass).attr('disabled','disabled');
$(countryClass).change(function(){
var countrySelect = $(this),
country = countrySelect.val(),
dealerSelect = countrySelect.parents('.gform_fields').find(dealerClass);
if(country != 0){
dealerSelect.attr('disabled','disabled');
$.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');
}
});
} else {
dealerSelect.attr('disabled','disabled');
$('option',dealerSelect).removeAttr('selected');
}
});
});
</script>
<!-- THE FUNCTIONS.PHP -->
<?php
// GO DEALER COUNTRY
add_filter("gform_pre_render", "populate_dropdown_dealer_country");
add_filter("gform_admin_pre_render", "populate_dropdown_dealer_country");
function populate_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-country';
$field["choices"] = $items;
}
}
return $form;
}
// GO DEALER NAME
add_filter("gform_pre_render", "populate_dropdown_dealer_name");
add_filter("gform_admin_pre_render", "populate_dropdown_dealer_name");
function populate_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;
}
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...','mission-theme'), "value" => 0 );
foreach($dealers as $dealer){
$items[] = array( "text" => $dealer->post_title, "value" => $dealer->post_name );
}
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');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment