I'm currently developing a system that requires the ability to remove employees from their Saturday shifts. This process involves clicking on an icon that triggers the JavaScript function "removeEmpFromSaturday" and passes relevant parameters.
Within this script, an Ajax request is made to update values in the database and subsequently remove the employee from their Saturday shift.
However, it seems that the PHP page I have specified is not being called or requested ("As indicated by Alerts on the PHP page").
Since I am still relatively new to AJAX, I acknowledge that my syntax for this particular function may be incorrect. Hence, any extra guidance would be highly appreciated.
Upon checking, when I perform an alert for the values "id, loc, week, year," they all display the expected correct values. Therefore, the issue does not seem to lie there.
The section of code where I suspect the problem exists:
<script>
function removeEmpFromSaturday(id, loc, week, year){
xhttp = new XMLHttpRequest();
xhttp.open("GET", "includes/ajax/remove_emp_from_saturday.php?e_id=" + id +
"&location=" + loc + "&week=" + week + "&year=" + year, false);
xhttp.send();
resetPlanner();
}
</script>
The PHP script I am referencing:
<?php require_once dirname(__FILE__)."/../admin_header.php"; ?>
<script>alert("STARTED");</script>
<?php
if(isset($_REQUEST['e_id'])){
$emp_id = escape($_REQUEST['e_id']);
$loc = escape($_REQUEST['location']);
$week = escape($_REQUEST['week']);
$year = escape($_REQUEST['year']);
$query = "SELECT e_hp_daily_pat FROM employees WHERE e_id = '{$emp_id}'";
$get_hp_daily_pat_query = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($get_hp_daily_pat_query);
$e_hp_daily_pat = escape($row['e_hp_daily_pat']);
$query = "SELECT * FROM slots WHERE s_location = '{$loc}' AND s_day = '6'
AND s_week = '{$week}' AND s_year = '{$year}'";
$get_emps_query = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($get_emps_query);
$s_real_sub = escape($row['s_real_sub']);
$s_emps = escape($row['s_emps']);
$s_emps = explode(";", $s_emps);
$process = false;
$e_match = 0;
foreach($s_emps as $emp){
if($emp == $emp_id){
unset($s_emps[$e_match]);
$process = true;
}
$e_match++;
}
if($process == true){
$s_emps = implode(";", $s_emps);
$s_real_sub -= $e_hp_daily_pat;
$query = "UPDATE slots SET s_emps = '{$s_emps}', s_real_sub = '{$s_real_sub}' WHERE s_location = '{$loc}' AND s_day = '6' AND s_week = '{$week} AND s_year = '{$year}'";
$set_emps_query = mysqli_query($connection, $query);
}
}
?>
<script>alert("COMPLETE");</script>
Before addressing it, I am aware that I have not bound my parameters which is somewhat outdated in mysqli. I will make sure to update that at a later stage.