I am trying to create a dependency for a project, the SQL query works and returns the data I need in PHP Admin however when I try to create the AJAX request for this all it does is return an empty array, this is called on a button press and when I change the AJAX call to another PHP file, it returns the data from that call but doesn’t return anything when I change it back again
JS Function
The “Id” here is filled in dynamically from an onClick function, this works and returns the correct id for the given location, checked in Chrome Dev Tools Network and can see the number is working
function checkLocationDependancies(id) {
$.ajax({
url: "./libs/php/checkDepartmentByLocation.php",
type: "GET",
dataType: 'json',
data: {
id,
},
success: function(result) {
console.log(id)
console.log(result)
},
error: function(result) {
alert('error');
}
})
};
PHP Route
SQL query below works, if i copy and paste it into PHP admin, it returns the required information I need
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$executionStartTime = microtime(true);
include("config.php");
header('Content-Type: application/json; charset=UTF-8');
$conn = new mysqli($cd_host, $cd_user, $cd_password, $cd_dbname, $cd_port, $cd_socket);
if (mysqli_connect_errno()) {
$output['status']['code'] = "300";
$output['status']['name'] = "failure";
$output['status']['description'] = "database unavailable";
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
$output['data'] = [];
mysqli_close($conn);
echo json_encode($output);
exit;
}
$query = $conn->prepare('SELECT `department`.`locationID`, `location`.`name` FROM `department` LEFT JOIN `location` ON (`location`.`id` = `department`.`locationID`) WHERE `department`.`id` = ?');
$query->bind_param("i", $_REQUEST['id']);
$query->execute();
if (false === $query) {
$output['status']['code'] = "400";
$output['status']['name'] = "executed";
$output['status']['description'] = "query failed";
$output['data'] = [];
mysqli_close($conn);
echo json_encode($output);
exit;
}
$output['status']['code'] = "200";
$output['status']['name'] = "ok";
$output['status']['description'] = "success";
$output['status']['returnedIn'] = (microtime(true) - $executionStartTime) / 1000 . " ms";
$output['data'] = [];
mysqli_close($conn);
echo json_encode($output);
?>
I have changed the route to another route and the data is returned on the button press, it should return a json response containing the location id and name of the location so I can then use this to create a condition to check and then accept or decline the delete
Alex Holmes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.