I am trying to create a filter with two dropdowns, one with shows and another with their locations. When a show is selected in the first dropdown the list of locations in the second dropdown gets modified to contain only the locations where that particular show is played. Now the problem I have is that if a user clicks the locations dropdown to fast after selecting a show, it sometimes updates only after the user made a selection modifying the list and thus removing their selection (This happens often in edge). Is there a way to update the list in the locations dropdown faster? Or atleast force it to update even though it is selected?
This the JavaScript I am using, it uses the ‘example_ajax_request’ function wich I have added to functions.php in WordPress you can see it below the script.
$('#select_show').change( function(){
var show = $("#select_show :selected").text();
$.ajax({
type: "post",
url: ajaxurl,
data: {
'action':'example_ajax_request',
'show' : show
},
dataType:"json",
success:function(data) {
console.log("what is happening al of a sudden");
console.log(data);
$('#province').children('option').remove();
for(var i = 0; i < data.length; i++)
{
if( data[i] == 'Choose a province')
var value = "all";
else
var value = data[i];
$('#province').append( $("<option value=" + value +"></option>").text(data[i]) );
}
},
error: function(errorThrown){
console.log(errorThrown);
}
});
});
My function wich gets the list of locations(provinces)
function example_ajax_request() {
$arrayList = array();
$arrayList[] = "Choose a province";
$selected = $_POST['show'];
if( $selected == "Choose a show" )
$selected = "";
$arg = array(
'posts_per_page' => -1,
'post_type' => 'address',
'meta_key' => 'show',
'meta_value' => $selected,
);
$the_query = new WP_Query( $arg );
if( $the_query->have_posts() )
{
while ( $the_query->have_posts() )
{
$the_query->the_post();
$arrayList = get_option_list('province', $arrayList);
}
}
print json_encode($arrayList);
wp_reset_query();
die();
}
add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' );
add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );
And these are the dropdowns and their queries
<?php
$arg = array(
'post_type' => 'address',
'posts_per_page' => -1,
'meta_key' => 'show',
'meta_value' => ''
);
$the_query = new WP_Query( $arg );
$showlist = array();
if( $the_query->have_posts() )
{
while ( $the_query->have_posts() )
{
$the_query->the_post();
$showlist = get_option_list('show', $showlist);
}
}
$arg = array(
'post_type' => 'address',
'posts_per_page' => -1,
'meta_key' => 'show',
'meta_value' => '',
);
$the_query = new WP_Query( $arg );
$provincelist = array();
if( $the_query->have_posts() )
{
while ( $the_query->have_posts() )
{
$the_query->the_post();
$provincelist = get_option_list('province', $provincelist);
}
}
?>
<div class="block-go-to">
<form action="reserve-tickets" method="get">
<select id="select_show" name="show">
<option value="all">Choose a show</option>
<?php
foreach($showlist as $show)
{
?>
<option value="<?php echo $show; ?>">
<?php echo $show; ?>
</option>
<?php
}
?>
</select>
<select id="province" name="province">
<option value="all">Choose a province</option>
<?php
foreach($provincelist as $province)
{
?>
<option value="<?php echo $province; ?>">
<?php echo $province; ?>
</option>
<?php
}
?>
</select>
<button id="show_map" class="cta right">Show map</button>
</form>
</div>
<?php
session_start();
if(isset($_POST['province']))
$_SESSION['province'] = $_POST['province'];
?>
add this just as you enter the change event of your
#select_show
selectthis will disable the select, meaning the user won’t be able to click on it manually (this is open to javascript hacks, tho).
Then when you’re sure all your locations have been fetched, just turn it back to false
which will of course re-enable the input to let the user use it.
note that there’s probably alot of other ways to work around that problem, but that way was just the first that came up to my mind!