Emm Emm - 1 month ago 11
PHP Question

Php Alert doesn't show

good afternoon. I have a Modal Form and the data is send via Ajax. The problem is that I try to know if the data already exist in data base and then get an alert. In my database the data is not repeating, but the alert is not appearing.

Part of Php code:

require 'puenteConexion.php';
// ***------------------------------------------------***
$proceso = $_POST['pro'];
$id = $_POST['id-paciente'];
$nombre = $_POST['nombre'];
$apellido_pat = $_POST['apellido_pat'];
$apellido_mat = $_POST['apellido_mat'];
$sexo = $_POST['sexo'];
$nacimiento = $_POST['nacimiento'];

$total = $conexion->query(" SELECT COUNT(*) FROM tbl_paciente WHERE nombre='$nombre' and apellido_pat='$apellido_pat' and nacimiento='$nacimiento' ");
$data_exists = ($total->fetchColumn() > 0) ? true : false;

if($data_exists){
die('The person exist');
}else{

//VERIFICANDO EL PROCESO

switch($proceso){
case 'Registro':
$conexion->query("INSERT INTO tbl_paciente (id_paciente, nombre, apellido_pat, apellido_mat, sexo, nacimiento) VALUES (null,'$nombre','$apellido_pat','$apellido_mat','$sexo','$nacimiento')");
break;

case 'Edicion':
$conexion->query("UPDATE tbl_paciente SET nombre = '$nombre', apellido_pat = '$apellido_pat', apellido_mat = '$apellido_mat',
sexo = '$sexo', nacimiento = '$nacimiento' WHERE id_paciente = '$id'");
break;
}

}


This is the function that return my Form on submit:

function agregaRegistro(){
var url = 'modal/php/agrega_paciente.php';
$.ajax({
type:'POST',
url:url,
data:$('#formulario').serialize(),
success: function(){
if ($('#pro').val() == 'Registro'){
$('#formulario')[0].reset();
$('#modal-mensaje').addClass('bien').html('Registro completado con exito').show(200).delay(1600).hide(200);
$('#pro').val('Registro');

return false;
}else{
$('#modal-mensaje').addClass('bene').html('Edicion completada con exito').show(200).delay(1600).hide(200);

return false;
}
}
});
return false;
};


Sorry, I edited the question, I hope to be more clear.
Thanks for your attention

Answer

You are not using any response in your ajax request. you need to get response as:

success: function(response){

Don't know why are you using die() here, you can just use echo here as:

if($data_exists){
    echo 'The person exist';
}else{
    // else part
}

But, still how can you check either getting success or error in ajax response. you can use json here like:

if($data_exists){
    echo json_encode(array('status'=>0,'message'=>'The person exist'));    // for error 
}else{
    // else part
    echo json_encode(array('status'=>1,'message'=>'Success message')); // for success
}

After this, you need to change your ajax as:

$.ajax({
    type:'POST',
    url:url,
    data:$('#formulario').serialize(),
    dataType: "json",  // will return json response
    success: function(response){
        if(response.status){
            // your success part
        }
        else{
            alert(response.message); // error response in alert
        }
    }
});

Other Improvements:

You can use Prepared Statement for preventing your SQL Statements with SQL Injection.