In the SweetAlert located in “kardex.js”, I am receiving this for some reason on the server:
[]{"justification":"Hello"}
And I want to receive only the text that corresponds to “justificatin”, which in this case is “Hello”. Please consider that I am not very proficient in JavaScript. Thank you in advance.
I have four segments of 4 files that are related to each other.
First, here is “kardex.js”:
if(info.event.title === "OMISSION")
{
getJustification(info.event.id, function(response)
{
Swal.fire(
{
title: 'Delete omission event?',
text: 'Justification: ' + response,
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete',
cancelButtonText: 'Cancel'
}).then((result) =>
{
if (result.isConfirmed)
{
eliminarOmision(info.event.id);
var event = calendar.getEventById(info.event.id); // Get the event by its ID
if (event)
{
event.remove(); // Remove the event from the calendar view
}
}
});
});
}
Next is “omission.ajax.js”:
function getJustification(eventId, callback)
{
$.ajax(
{
type: 'POST',
url: 'controller/omission.controller.php',
data:
{
accion: 'justification',
eventId: eventId
},
success: function(response)
{
callback(response);
},
error: function()
{
Swal.fire(
{
title: 'Error obtaining justification',
text: 'There was an error obtaining the justification for the event.',
icon: 'error',
confirmButtonText: 'Accept'
});
}
});
}
Then “omission.controller.php”:
<?php
require_once "../model/omission.model.php";
class Controller
{
public static function manageRequest()
{
$response= array();
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$table= array(0=>"omission",
1=>"events");
if (isset($_POST["action"]) && $_POST["action"] == "justification")
{
$eventId = $_POST["eventId"];
$response= Model::getJustification($table, $eventId);
}
}
echo json_encode($response);
}
}
Controller::manageRequest();
?>
Finally, “omission.model.php”:
public static function getJustification($table, $eventId)
{
$conn = connection::connect();
// Get justification
$stmt = $conn->prepare("SELECT justification FROM $table[0] WHERE id = :id");
$stmt->bindParam(":id", $eventId, PDO::PARAM_INT);
$stmt->execute();
$responose = $stmt->fetch(PDO::FETCH_ASSOC);
return $response;
}
What am I doing wrong? I have tried changing “response” to “response.justification”, but that gives me an error.