I have a system where I am using AJAX to insert data,
$.ajax({
data: formData, //send data via AJAX
url: '../controller/serviceController.php', //url file controller PHP
dataType: 'text', // what to expect back from the server
cache: false,
contentType: false,
processData: false,
type: 'post', //send POST data
beforeSend: function () {
document.getElementById("load").style.display = "block";
},
success: function (response) { //get request
var confirm = response.success;
if(confirm){
$("#successModalTitle").html("<i class='fas fa-check-circle color-success'></i> ¡Success!");
$("#successModalDescription").html(response.message);
}else{
$("#successModalTitle").html("<i class='fas fa-exclamation-circle color-error'></i> ¡Error!");
$("#successModalDescription").html(response.message);
}
//alert(response.message);
$('#actionModal').modal('toggle');
$('#services').DataTable().ajax.reload();
$('.close').click();
}
});
I send it all the data I need in my PHP file, I go to the PHP file, I create variables that I am going to use and I insert my data, THE QUERY IS CORRECT because it does INSERT,
$name = $_POST['name'];
$cost = $_POST['cost'];
$desc = $_POST['desc'];
$id_tipo_servicio = $_POST['id_tipo_servicio'];
$date = date("Y-m-d");
$iva = 0;
$status = 1;
$nombre = $_FILES['archivo']['name'];
//$ruta = $_FILES['archivo']['tmp_name'];
$destino = ".../img/" . $nombre;
require_once 'conn/connection.php';
$connect = new connection();
$connection=$connect->connections();
$sql = "INSERT INTO servicios (id_tipo_servicio, ser_nombre, ser_descripcion, ser_img_url, ser_precio, ser_fecha_creacion, ser_fecha_actualizacion, ser_iva, ser_status) VALUES ('".$id_tipo_servicio ."','".$name."','".$desc."','".$destino."','".$cost."','".$date ."', '".$date."', '".$iva."', '".$status."');";
$jsondata = array();
$insert = $connection->query($sql);
var_dump($insert);
if ($insert===true) {
$jsondata['success'] = true;
$jsondata['message'] = 'Felicidades! Has insertado el Servicio con Éxito.';
} else {
$jsondata['success'] = false;
$jsondata['message'] = 'Error! Ha ocurrido un error, avisa a un administrador.';
}
//Aunque el content-type no sea un problema en la mayoría de casos, es recomendable especificarlo
header('Content-type: application/json; charset=utf-8');
echo json_encode($jsondata, JSON_FORCE_OBJECT);
So, having this correct, I do an IF, where I assign values to the ARRAY that my PHP returns in JSON format so that my AJAX can READ it back.
$insert = $connection->query($sql);
var_dump($insert);
if ($insert===true) {
$jsondata['success'] = true;
$jsondata['message'] = 'Felicidades! Has insertado el Servicio con Éxito.';
} else {
$jsondata['success'] = false;
$jsondata['message'] = 'Error! Ha ocurrido un error, avisa a un administrador.';
}
So when I'm back in the AJAX, instead of showing the PHP results, it shows my error alert,
But the ANSWER IS CORRECT. The AJAX must have entered this part of the code
var confirm = response.success;
if(confirm){
$("#successModalTitle").html("<i class='fas fa-check-circle color-success'></i> ¡Success!");
$("#successModalDescription").html(response.message);
}
BUT IT'S NOT LIKE THAT. It enters the ERROR section and it does not show me the description of the response.
The problem is that you have defined
dataType
astext
and should bejson
example: