Greetings friends, I am currently solving an exercise in which I want to make a query without having to reload the page, for this reason I am using ajax ... But I have been several hours and nothing that I have with the solution, e watched some examples that exist on the internet but not even that and managed to solve it.
The problem is that the browser does not give me any error, but it hangs on executing the query and I must close the tab to return it to normal.
What I want to do is insert a cedula number and when clicking on a button, check if it exists in my database, if it exists, you should bring me some values that will be assigned in several text boxes
This is the code that I have in the head of the page
<script type="text/javascript" src="../js/jquery-2.2.4.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// Asigno un evento a un botón de mi formulario
$("[name='buscar_cli']").click(function(e){
e.preventDefault();
var datos_enviados = {
'buscar_cli' : $("[name='ced_cli']")
}
$.ajax({
type:'POST',
url: 'bd_comun.php',
data: datos_enviados,
datatype: 'json',
})
// Compruebo si me esta trayendo los valores
.done(function(data){
console.log(data);
var datos2 = JSON.parse(data);
console.log(datos2);
})
})
})
</script>
I send the form to a php script
if(isset($_POST['buscar_cli'])){
$persona = new usuario();
$valor = $persona->buscarCliente($_POST['buscar_cli']);
}
Finally sent the data to another php script to make the query
public function buscarCliente($ced){
$buscar = "SELECT COUNT(*) FROM registro_clientes WHERE rc_cedu=:a";
$resultado = $this->db_conexion->prepare($buscar);
$resultado->execute(array(':a'=>$ced));
$filas = $resultado->fetchColumn();
if($filas>0){
$extraer = "SELECT rc_cedu,rc_nomb,rc_aped,rc_telf,rc_dire FROM registro_clientes WHERE rc_cedu=:a";
$resultado2 = $this->db_conexion->prepare($extraer);
$resultado2->execute(array(':a'=>$ced));
$data["datos"][] = $resultado2->fech(PDO::FETCH_ASSOC);
echo json_encode($data);
$resultado2->closeCursor();
}else{
echo 'Error';
}
$resultado->closeCursor();
$this->db_conexion = null;
}
That's what I have friends, I comment that my experience with ajax is null. If you can help me, I would appreciate it, I have already modified the exercise several times but I can not make it work: (