After upgrading PHP from 5.6 to 7.2 my ajax request no longer works and throws error 400 Bad Request.
At first I thought it was a file permission error because my code worked fine on my localhost but when I pushed it up to my demo server it failed the ajax request. Several wasted hours of my life later I noticed my localhost was running PHP 5.6. I updated and behold the same error I was seeing on the demo server presented itself.
My main question is: what is different about PHP 7.2 in terms of ajax requests that my code from 5.6 no longer works?
The PHP form request:
<div class="search-bar">
<form action="<?php echo site_url(); ?>/wp-admin/admin-ajax.php" method="GET" id="staff-filter">
<div class="input-set">
<label>keyword</label>
<input type="text" name="search" id="search" value="" placeholder="I'm looking for..." />
</div>
<?php if( $terms = get_terms( array(
'taxonomy' => 'department',
'orderby' => 'name',
) ) ) :
echo '<div class="input-set">';
echo '<label>department</label>';
echo '<select name="department-filter"><option value=" "></option>';
foreach( $terms as $term ) :
echo '<option value="'. $term->term_id .'">' . $term->name . '</option>';
endforeach;
echo '</select>';
echo '</div>';
endif;?>
<a class="btn" id="apply-staff-filter" href="">Go</a>
<input type="hidden" name="action" value="stafffilter" />
</form>
</div>
<div class="search-results" id="staff-response">
</div>
The response:
function filter_staff() {
$args = array(
'orderby' => 'name',
'post_type' => 'staff',
);
if( isset( $_GET['department-filter'] ) ) :
$args['tax_query'] = array(
array(
'taxonomy' => 'department',
'field' => 'id',
'terms' => $_GET['department-filter']
)
);
endif;
if( isset( $_GET['search'] ) ) :
$search = sanitize_text_field( $_GET['search']);
$query = new WP_Query( array(
'post_type' => 'staff',
'tax_query' => $args['tax_query'],
's' => $search,
));
else :
$query = new WP_Query( $args );
endif;
if( $query->have_posts() ) :
while( $query->have_posts() ) : $query->the_post(); ?>
<?php $about = get_field('about'); ?>
<?php $info = get_field('information'); ?>
<div class="staff-card">
<div class="staff-top">
<img src="<?php echo $about['photo']; ?>" />
</div>
<div class="staff-bot">
<p><?php echo $query->post->post_title; ?></p>
<p><?php echo $info['title']; ?></p>
<a href="tel:<?php echo $info['phone']; ?>"><?php echo $info['phone']; ?></a>
<a href="mailto:<?php echo $info['email']; ?>"><?php echo $info['email']; ?></a>
<a class="staff-bio btn" href="<?php the_permalink(); ?>">BIO</a>
</div>
</div>
<?php endwhile;
wp_reset_postdata();
else :
echo '<p>We could not find what you were looking for.</p>';
endif;
die();
}
add_action( 'wp_ajax_stafffilter', 'filter_staff' );
add_action( 'wp_ajax_nopriv_stafffilter', 'filter_staff' );
and the JS:
function staff_filter() {
var filter = $('#staff-filter');
$.ajax({
url: filter.attr('action'),
data: filter.serialize(),
type: filter.attr('method'),
beforeSend : function(xhr) {
filter.find('button').text('Searching...');
},
success: function(data) {
filter.find('button').text('Apply Filter');
$('#staff-response').html(data);
}
});
return false;
}
$( document ).ready(function() {
staff_filter();
});
$('#apply-staff-filter').on('click', function(e) {
e.preventDefault();
staff_filter();
});
// Prevent Enter Submit on Staff Directory Search
$('#search').keypress(function(event) {
if( event.keyCode == 13 ) {
event.preventDefault();
}
});