Friends please I need help I do not know why ajax does not bring me the result of the request when I send data from my form. If someone at least explain to me what is my mistake and what is it about? Thanks.
What I know, is that when I send my data to .php through ajax inserted to the asuario and indeed it does but in my condition where it validates if the query was executed is my variable that awaits the result that I must take to the html, but nothing happens. He does not show me the alert and he still shows me that error when I reload on another page:
My code:
document.getElementById("enviar").onclick = function() {
RegistrarUsuario();
};
String.prototype.isEmpty = function() {
return (this.length === 0 || !this.trim());
};
function RegistrarUsuario(){
var comprobar = false;
//Datos proporcionado por el usuario
js_matricula =$("#matricula_1").val();
js_nombre =$("#nombre_1").val();
js_apellido_pa =$("#apellido_pa_1").val();
js_apellido_ma =$("#apellido_ma_1").val();
js_carrera = $('#id_carrera').val();
var arreglo = new Array(js_matricula, js_nombre,js_apellido_pa,js_apellido_ma);
for(var i=0; i<arreglo.length; i++){
if(arreglo[i].isEmpty()){
comprobar = true;
break;
}
}
if(comprobar){
alert("Por favor, no deje ningun campo sin texto.");
return;
}
var request = $.ajax({
url: "Verificacion/Validar_Entrada.php",
method: "POST",
data:{ matricula:js_matricula, nombre:js_nombre, apellido_pa:js_apellido_pa, apellido_ma:js_apellido_ma, id_carrera: js_carrera},
dataType:"jsonp",
jsonp:"jsoncallback",
crossDomain: true,
cache: false
});
request.done(function( data ) {
if(data.estado=="exito"){
alert("Muy bien");
}else{
alert("Algo inesperado a ocurrido, por favor intentelo más tarde.");
}
});
request.fail(function( jqXHR, textStatus ) {
alert( "Error en la petición: " + textStatus );
});
return false;
}
My .php
<?php
header("Content-type: application/json");
$matricula = $_GET['matricula'];
$nombre = $_GET['nombre'];
$apellido_pa = $_GET['apellido_pa'];
$apellido_ma = $_GET['apellido_ma'];
$id_carrera = $_GET['id_carrera'];
$id_carrera = (!empty($id_carrera)) ? $id_carrera : NULL ;
$datos = array();
$nombre = trim($nombre , " ");
$apellido_pa = trim($apellido_pa , " ");
$apellido_ma = trim($apellido_ma , " ");
//Deja solo un espacio en blanco entre palabras
$nombre = preg_replace( "([ ]+)"," ",$nombre);
$apellido_pa = preg_replace( "([ ]+)"," ",$apellido_pa);
$apellido_ma = preg_replace( "([ ]+)"," ",$apellido_ma);
include_once('Conexion/Abrir_Conexion.php');
$sql = "INSERT INTO alumno (matricula, nombre, apellido_pa, apellido_ma, id_carrera)
VALUES
(?, ?, ?, ?, ?)";
$sentencia=$conexion->prepare($sql);
$sentencia->bind_param("isssi", $matricula, $nombre, $apellido_pa, $apellido_ma, $id_carrera);
if($sentencia->execute()){
$datos["estado"] = "exito";
}else{
$datos["estado"] = "error";
}
$sentencia->close();
$conexion->close();
$resultadoJson = json_encode($datos);
echo $_GET['jsoncallback'] . '(' . $resultadoJson . ');';
Friends make changes that you suggested to me but now it does not send me the error, I do not know if it is the dataType, because even then it does not send me the alert (that if it is right or wrong or the error throws me); just insert records reload the page and again again. What's happening ???