i am trying to get data via ajax. everything works fine except the "error handler". whenever i have error in my php i want that the error is sent to the html page (javascript file). it's not working.
im using the success in my ajax.
What i want is to get alerted with the msg and error ive set in the PHP file (line 5 and 6)
Here is my code:
PHP
$result = mysqli_query($conn, $sql);
if (!$result || mysqli_num_rows($result) == 0) {
$resp = array(
'status' => "Error",
'msg' => "Error Msg",
'error' => mysqli_error($conn)
);
echo json_encode($resp);
die();
} else {
while ($row = mysqli_fetch_array($result)) {
$id = $row['id'];
// $ownId = $row['own_id'];
$name = $row['name'];
$geraet = $row['manuf'].' '.$row['model'];
$fail = $row['fail'];
$abg_dat = $row['pick_date'];
$status = $row['status'];
$return_arr[] = array("id" => $id,
"name" => $name,
"geraet" => $geraet,
"fail" => $fail,
"abg_dat" => $abg_dat,
"status" => $status
);
}
$resp = array(
'status' => "Succ" ,
'msg' => "Daten erfolgreich abgerufen",
'resp' => $return_arr
);
echo json_encode($resp);
}
JS
$.ajax({
url: `${phpDir}/inc/techniker.inc.php`,
type: 'get',
dataType: 'JSON',
success: function(response){
if (response['status'] == "Error") {
alert(response['status'] + "<br/>" + response['error']);
} else {
var len = response['resp'].length;
var rspKey = response['resp'];
for(var i=0; i<len; i++){
var id = rspKey[i].id;
var name = rspKey[i].name;
var geraet = rspKey[i].geraet;
var stoerung = rspKey[i].fail;
var abgabe = rspKey[i].abg_dat;
var status = rspKey[i].status;
// example on muliple rows
// var username = response[i].username;
var tr_str = `
<tr id="${id}" onclick="tabCaller(${id})">
<td>#${id}</td>
<td>${name}</td>
<td>${geraet}</td>
<td>${stoerung}</td>
<td>${abgabe}</td>
<td>${status}</td>
</tr>
`;
$("#techniker_tbody").append(tr_str);
}
}
}
});
UPDATE when mysqli error is happening the php is breaking and showing the error before he sends the response. that why im not gettin the 'error' either the 'msg'