I created a small script to handle a dropdown select that retrieves data from a custom MySQL table. The script uses admin-ajax.php, and it works perfectly on my local MAMP. However when I uploaded the files on Bluehost server I have a 500 status for the request if I am logged in as administrator. If I ma not logged in, I have a 400 bad request. Moreover the page appears with the administration top bar, even if I am not logged in and it appears to other users as well. I have no idea of what happened
Here's my code
<script>
jQuery(document).ready(function($) {
// Event listener for changes in the first dropdown (#region_filter)
$('#region_filter').on('change', function() {
// Fetch selected region values
var selectedRegions = $(this).val();
// Make an AJAX request to retrieve corresponding areas for selected regions
$.ajax({
url: "domain.org/wp-admin/admin-ajax.php",
type: 'POST',
data: {
action:'eAtlasFiltersRegions',
regions: selectedRegions
},
success: function(response) {
// Parse the JSON response
var areas = JSON.parse(response);
// Clear and update the third dropdown (#isra_filter) options
var israDropdown = $('#isra_filter');
israDropdown.empty();
israDropdown.append($('<option value="">Search area names</option>'));
israDropdown.append($('<option value="all">SELECT ALL</option>'));
// Add retrieved area names to the dropdown
areas.forEach(function(area) {
israDropdown.append($('<option value="' + area + '">' + area + '</option>'));
});
// Refresh the dropdown to apply changes
israDropdown.dropdown('refresh');
}
});
});
});
</script>
and on the functions.php I wrote the following:
add_action('wp_ajax_eAtlasFiltersRegions', 'eAtlasFiltersRegions');
function eAtlasFiltersRegions(){
global $wpdb;
if (isset($_POST['regions'])) {
// Get the selected regions from the AJAX request
$selectedRegions = $_POST['regions'];
console.log($selectedRegions);
// Construct the SQL query to retrieve areas based on selected regions
$query = "SELECT DISTINCT area_name FROM isra_map_area_names WHERE region_code IN (SELECT region_code FROM isra_map_regions WHERE region IN (";
$regionValues = array_map(function($region) {
// Sanitize and escape each selected region
return "'" . addslashes($region) . "'";
}, $selectedRegions);
$query .= implode(",", $regionValues) . ")) ORDER BY `area_name` ASC";
// Execute the query
$query_results = $wpdb->get_results($query);
// Extract area names from the query results
$areaNames = array();
foreach ($query_results as $result) {
$areaNames[] = stripslashes($result->area_name);
}
// Return the area names as a JSON response
echo json_encode($areaNames);
} else {
// Return an empty JSON response if no regions are selected
if (isset($_POST['regions']) & $_POST['regions'] =='') {
$query_area_name = $wpdb->get_results("SELECT * FROM isra_map_area_names ORDER BY `area_name` ASC", ARRAY_A);
// Extract area names from the query results
$areaNames = array();
foreach ($query_results as $result) {
$areaNames[] = stripslashes($result->area_name);
}
// Return the area names as a JSON response
echo json_encode($areaNames);
}
}
wp_die();
}
The wordpress environment is the same on local and on server, as well as the plugins. Has anyone found the same issue? What is the mistake in my code?