nfsarmento
8/24/2016 - 3:46 PM

Populate second dropdown based on first

Populate second dropdown based on first

									  <script type="text/javascript">
										jQuery(document).ready(function($) {
											$('#as_areas').change(function()
											{
											    var $as_areas=$('#as_areas').val();
											    // call ajax
											    $("#sub_cat").empty();
											    $.ajax
											    (
											        {
											            url:'<?php echo admin_url('admin-ajax.php'); ?>',
											            type:'POST',
											            data:'action=after_schools_ajax_call&after_school_area=' + $as_areas,

											            success:function(results)
											            {
											                $("#sub_cat").removeAttr("disabled");
											                $("#sub_cat").append(results);
											            }
											        }
											    );
											});
											var sel = document.getElementById('sub_cat');
											sel.onchange = function () {
											    document.getElementById("go_button").href = this.value;
											}
										});
										</script>



									<div id="school-selector">

										<h2>Select Your School</h2>
										<?php
										 wp_dropdown_categories('show_count=0&show_option_all=Choose your area&selected=-1&hierarchical=1&depth=1&hide_empty=0&exclude=1&name=as_areas&taxonomy=after_school_area');
										?>
										<select name="sub_cat" id="sub_cat" disabled="disabled"></select>
										<a id="go_button" class="button" href="#"><?php _e( 'Go', 'bonestheme'); ?></a>
									</div>
<?php
function tend_after_schools_ajax() {
	if ( isset( $_POST[ 'after_school_area' ] ) ) {
		$school_term_ID = $_POST[ 'after_school_area' ];
		$schools_array = get_posts(
			array(
					'posts_per_page' => -1,
					'post_type' => 'page',
					'orderby' => 'title',
				    'order' => 'ASC',
					'tax_query' => array(
						array(
						'taxonomy' => 'after_school_area',
						'terms' => $school_term_ID,
					),
				)
			)
		);
		foreach ( $schools_array as $school ) {
			  $school_id = $school->ID;
			  $option .= '<option value="' . get_permalink( $school_id ) . '">';
	          $option .= $school->post_title;
	          $option .= '</option>';
		}
		echo '<option value="-1" selected="selected">Choose your school</option>'.$option;
	    die();
	}
}
add_action('wp_ajax_after_schools_ajax_call', 'tend_after_schools_ajax');
add_action('wp_ajax_nopriv_after_schools_ajax_call', 'tend_after_schools_ajax');
<?php
if ( ! function_exists( 'tend_after_school_areas' ) ) {

// Register Custom Taxonomy
function tend_after_school_areas() {

	$labels = array(
		'name'                       => _x( 'After School Areas', 'Taxonomy General Name', 'text_domain' ),
		'singular_name'              => _x( 'After School Area', 'Taxonomy Singular Name', 'text_domain' ),
		'menu_name'                  => __( 'After School Areas', 'text_domain' ),
		'all_items'                  => __( 'All After School Areas', 'text_domain' ),
		'parent_item'                => __( 'Parent After School Area', 'text_domain' ),
		'parent_item_colon'          => __( 'Parent After School Area:', 'text_domain' ),
		'new_item_name'              => __( 'New After School Area Name', 'text_domain' ),
		'add_new_item'               => __( 'Add New After School Area', 'text_domain' ),
		'edit_item'                  => __( 'Edit After School Area', 'text_domain' ),
		'update_item'                => __( 'Update After School Area', 'text_domain' ),
		'view_item'                  => __( 'View After School Area', 'text_domain' ),
		'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
		'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
		'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
		'popular_items'              => __( 'Popular Items', 'text_domain' ),
		'search_items'               => __( 'Search Items', 'text_domain' ),
		'not_found'                  => __( 'Not Found', 'text_domain' ),
		'no_terms'                   => __( 'No items', 'text_domain' ),
		'items_list'                 => __( 'Items list', 'text_domain' ),
		'items_list_navigation'      => __( 'Items list navigation', 'text_domain' ),
	);
	$args = array(
		'labels'                     => $labels,
		'hierarchical'               => false,
		'public'                     => true,
		'show_ui'                    => true,
		'show_admin_column'          => true,
		'show_in_nav_menus'          => true,
		'show_tagcloud'              => false,
	);
	register_taxonomy( 'after_school_area', array( 'page' ), $args );

}
add_action( 'init', 'tend_after_school_areas', 0 );

}