Skip to content

Instantly share code, notes, and snippets.

@ricardosiri68
Created October 23, 2013 23:50
Show Gist options
  • Save ricardosiri68/7128881 to your computer and use it in GitHub Desktop.
Save ricardosiri68/7128881 to your computer and use it in GitHub Desktop.
validation and ajax call 2
<?php
include("conn.php");
$nombre= $_POST['nombre'];
$apellido= $_POST['apellido'];
$user = $_POST['user'];
$pass= $_POST['pass'];
$email = $_POST['email'];
//Comprueba si el Usuario ya existe en la BD
$sql="SELECT user,email FROM usuarios WHERE user = $user OR email = $email";
$res = mysql_query($sql, $link);
$numRow = ($res) ? mysql_num_rows($res) : 0;
if($numRow != 0){
$row = mysql_fetch_assoc($consulta);
$resp = false;
$field = ($row["user"] == $user) ? "user" : "mail";
$msj = ($field == "user") ? "<span class='fail'><p>Ese Usuario ya está registrado.</p></span>" : "<span class='fail'><p>Ese Email está siendo usado por otro Usuario.</p></span>";
}else{
$resp = true;
$field = "";
$msj = "<span class='ok'><p>Se ha registrado correctamente.</p></span>";
}
$arr = array(
"registro"=>$resp,
"msg"=> $msj,
"campo" => $field
);
echo json_encode($arr);
?>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<form action="" method="POST" id="regForm">
<div id="resp"></div>
<input type="text" name="nombre" placeholder="Nombre" id="nombre">
<input type="text" name="apellido" placeholder="Apellido" id="apellido">
<input type="text" name="us" placeholder="Usuario" id="us">
<input type="password" name="pas" placeholder="Contraseña" id="pas">
<input type="email" name="mail" placeholder="Email" id="mail" >
<input type="button" value="Registrarse" href='#' class="button" onclick="javascript: registro(3)">
</form>
</body>
</html>
function crearXMLHttpRequest()
{
var xmlHttp=null;
if (window.ActiveXObject)
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
else
if (window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest();
return xmlHttp;
}
function highLightInput(input){
return function(){
input.style.border='1px solid #ccc';
}
}
function showFormWarnning(input,fieldName){
var msj = "<span class='fail'><p>Por favor completa el campo "+fieldName+".</p></span>";
showFowmWarnningStep2(input, msj);
}
function showFowmWarnningStep2(input, msj){
var detalles = document.getElementById("resp");
detalles.innerHTML = msj;
input.focus();
input.style.border="1px solid #E30022";
setTimeout(highLightInput(input), 1000);
}
function validateField(objField){
if(objField.input.value.length == 0){
showFormWarnning(objField.input, objField.fieldName);
return false;
}
return true;
}
function registro(act){
//
// Validar que todos lo campos esten completos
var form = document.getElementById("regForm");
var fields = [
{ input: form.nombre, fieldName: "Nombre"},
{ input: form.apellido, fieldName: "Apellido"},
{ input: form.us, fieldName: "Usuario"},
{ input: form.pas, fieldName: "Contraseña" },
{ input: form.mail, fieldName: "Email"}
];
if(fields.every(validateField))
{
conexion=crearXMLHttpRequest();
conexion.onreadystatechange = procesarEventos;
conexion.open('POST', 'ajax.php', true);
conexion.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
conexion.send(
'nombre='+ form.nombre.value+
'&apellido='+ form.apellido.value+
'&user='+ form.us.value+
'&email='+ form.mail.value+
'&pass='+ form.pas.value+
'&act='+ act);
}
}
function procesarEventos()
{
var detalles = document.getElementById("resp");
if(this.readyState == 4)
{
console.log(this.responseText);
var resp = JSON.parse(this.responseText);
var registro= resp.registro;
var msg= resp.msg;
var campo = resp.campo;
if (registro) { //Si no hay errores, se procede a avisar del registro correcto
detalles.innerHTML = msg;
document.getElementById("regForm").reset();
}else{
if(campo == 'user'){ //Si el usuario esta registrado
var input = document.getElementById("regForm").us;
}else if (campo == 'email'){ // Si el email ya esta registrado, se envia el mensaje
var input = document.getElementById("regForm").mail;
}
showFowmWarnningStep2(input, msg);
}
}else{
detalles.innerHTML = '<img src="img/loading.gif">';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment