Greetings, I'm having problems when making AJAX requests to PHP, using a connection to the db with PDO. At the moment of executing the Login, I am left Validating... and it does not give me any response. This is the code.
try{
$this->con = new PDO('mysql:host=localhost;dbname=poo_pdo','root','');
}catch(PDOExcepcion $e){
echo $e->getMessage();
exit;
}
AJAX:
jQuery(document).on('submit','#login_admin', function(event){
event.preventDefault();
jQuery.ajax({
url:'Controllers/procesar_login.php',
type: 'POST',
dataType: 'json',
data: $(this).serialize(),
beforeSend: function(){
$('#entrar').val('Validando...');
$('#entrar').attr("disabled", "disabled");
}
})
.done(function(respuesta){
if(respuesta.tipo == 'administrador'){
$('#alerta_ok').slideDown('slow');
setTimeout(function(){
$('#alerta_ok').slideUp('slow');
location.href = 'admin/admin.php';
},4000);
}else{
$('#alerta_err').slideDown('slow');
setTimeout(function(){
$('#alerta_err').slideUp('slow');
},4000);
$('#entrar').removeAttr("disabled");
$('#entrar').val('Entrar');
}
})
});
PHP:
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
require('../Conexion.php');
sleep(2);
$con->set_charset('utf8');
$usuario = $con->real_escape_string($_POST['admin_usuario']);
$pass = $con->real_escape_string($_POST['admin_clave']);
if($new_consulta = $con->prepare("SELECT tkd_admin.admin_nombre,
tkd_admin.admin_usuario, tkd_admin.admin_clave, tkd_admin.tkd_idperfil,
tkd_perfiles.perfiles FROM tkd_perfiles INNER JOIN tkd_admin
ON tkd_perfiles.idperfil = tkd_admin.tkd_idperfil
WHERE tkd_admin.admin_usuario = ? AND tkd_admin.admin_clave = ?")){
$new_consulta->bind_param('ss',$usuario,$pass);
$new_consulta->execute();
$result = $new_consulta->get_result();
if($result->num_rows>0){
$datos = $result->fetch_assoc();
$_SESSION['usuario'] = $datos;
echo json_encode(array('error'=>false,'tipo'=>$datos['perfiles']));
}else{
echo json_encode(array('error'=>true));
echo mysqli_error($con);
}
$new_consulta->close();
}
}
$con->close();
?>
That's all I have, as I said my button stays Validating, and it doesn't give me any response, the only thing I have is this that throws it to me in console
It doesn't even print the json for me, or anything. And for testing I have placed new connection mysqli and it works fine. I don't know if it's problems with the ajax I'm using. I would greatly appreciate your help on this.
The problem is that you haven't actually migrated your code to PDO. You still use functions
mysqli
with PDO.In PDO things are somewhat different from mysqli.
Try doing it this way.
I tell you the main changes:
prepare
, that's why it existsbind_param
it is not for PDO. It would bebindParam
, but it is not necessary either, since with PDO you can pass the parameters in the form of an array in itexecute
without any risk.get_result
is a mysqli method. With PDO you must use one of thefetch
. Here we will usefetchAll
, which would be the closest method toget_result
.errorInfo
.close
but the object instance is destroyed, assigning itNULL
. The same happens with the object that is obtained from the methodprepare
.I think that is the main thing.
Try this code. Assuming it connects fine, it should work.
Connection
Query code