Last active
August 29, 2015 13:56
-
-
Save nicolapiz/9310588 to your computer and use it in GitHub Desktop.
fichero que contiene la ingeniería ajax para llamar a un php e insertar un usuario a la base de datos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// JavaScript Document | |
// Función para recoger los datos de PHP según el navegador, se usa siempre. | |
function objetoAjax(){ | |
var xmlhttp=false; | |
try { | |
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); | |
} catch (e) { | |
try { | |
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); | |
} catch (E) { | |
xmlhttp = false; | |
} | |
} | |
if (!xmlhttp && typeof XMLHttpRequest!='undefined') { | |
xmlhttp = new XMLHttpRequest(); | |
} | |
return xmlhttp; | |
} | |
//Función para recoger los datos del formulario y enviarlos por post | |
function asignar(){ | |
id=document.frm1.id.value; | |
nom=document.frm1.name.value; | |
ape=document.frm1.surname.value; | |
//instanciamos el objetoAjax | |
ajax=objetoAjax(); | |
//uso del medotod POST | |
//archivo que realizará la operacion | |
//registro.php | |
ajax.open("POST", "setearVariablesSesion.php",true); | |
//cuando el objeto XMLHttpRequest cambia de estado, la función se inicia | |
ajax.onreadystatechange=function() { | |
//la función responseText tiene todos los datos pedidos al servidor | |
if (ajax.readyState==4) { | |
//LimpiarCampos(); | |
} | |
} | |
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); | |
//enviando los valores a registro.php para que inserte los datos | |
ajax.send("name="+nom+"&surname="+ape+"&id="+id) | |
} | |
//Función para recoger los datos del formulario y enviarlos por post | |
function enviarDatosUsuario(){ | |
id=document.frm1.id.value; | |
nom=document.frm1.name.value; | |
ape=document.frm1.surname.value; | |
//instanciamos el objetoAjax | |
ajax=objetoAjax(); | |
//uso del medotod POST | |
//archivo que realizará la operacion | |
//registro.php | |
ajax.open("POST", "Ej3.php",true); | |
//cuando el objeto XMLHttpRequest cambia de estado, la función se inicia | |
ajax.onreadystatechange=function() { | |
//la función responseText tiene todos los datos pedidos al servidor | |
if (ajax.readyState==4) { | |
if( ajax.responseText == 1){ | |
alert("El usuario existe"); | |
}else{ | |
LimpiarCampos(); | |
} | |
} | |
} | |
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); | |
//enviando los valores a registro.php para que inserte los datos | |
ajax.send("name="+nom+"&surname="+ape+"&id="+id) | |
} | |
//función para limpiar los campos | |
function LimpiarCampos(){ | |
document.frm1.name.value=""; | |
document.frm1.surname.value=""; | |
document.frm1.id.value=""; | |
document.frm1.name.focus(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment