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 “justification”, which in this case is “Hello”, and display it on this part:
text: 'Justification: ' + response,
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)
{
delete(info.event.id);
}
});
});
}
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);
}
});
}
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= "omission";
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”:
<?php
require_once "db.php";
class Model
{
public static function getJustification($table, $eventId)
{
$conn = connection::connect();
// Get justification
$stmt = $conn->prepare("SELECT justification FROM $table 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.