Ver Mensaje Individual
  #7 (permalink)  
Antiguo 19/08/2015, 10:50
kazukyx5
 
Fecha de Ingreso: julio-2015
Ubicación: Colombia Bogota
Mensajes: 72
Antigüedad: 8 años, 10 meses
Puntos: 0
Información Respuesta: Validar un registro antes de insertar

Cita:
Iniciado por Alexis88 Ver Mensaje
si
Cita:
Iniciado por mauricio1020 Ver Mensaje
si
Hey amigos si he leido todo paso a paso pero soy un poco lento y todo es nuevo es para mi , pero bueno he armado todo de esta forma, no se pero igual me deja pasar algunos datos repetidos y aveces no.

index.php
Código PHP:
<html>
  <
head>
  <
title></title>
  </
head>
  <
link rel="stylesheet" type="text/css" href="Codigo.CSS"
  <
style type="text/css"> </style>
  <
body>
  <
form id "datos" action "registrar.php" method "post">
    <
label for = "correo">Ingrese su correo electrónico para suscribirse a nuestras noticias:</label>
    <
input type "email" name "correo" id "correo" required />
    <
span id "mensaje"></span>
    <
input type "submit" value "Suscribirse" />
</
form>
  </
body>
  <
script type="text/javascript" src="Codigo.js">
  
</script>
  <script>
  document.addEventListener("DOMContentLoaded", function(){
    /* Aquí va el código */ }, false);
</script>
</html> 
Codigo.js
Código Javascript:
Ver original
  1. var form = document.querySelector("#datos"),
  2.     input = document.querySelector("#correo"),
  3.     span = document.querySelector("#mensaje"),
  4.     ajax, envio;
  5.  
  6. input.addEventListener("blur", function(){
  7.     ajax = new XMLHttpRequest();
  8.     ajax.open("GET", "comprobar.php?correo=" + this.value, true);
  9.     ajax.send();
  10.     ajax.addEventListener("load", function(){
  11.         if (this.status == 200){
  12.             if (this.responseText == "yes"){
  13.                 span.innerHTML = "El correo electrónico ingresado ya está registrado. Ingrese otro.";
  14.                 span.className = "invalid";
  15.                 envio = false;
  16.             }
  17.             else{
  18.                 span.innerHTML = "Continúe con el ingreso de datos.";
  19.                 span.className = "valid";
  20.                 envio = true;
  21.             }
  22.         }
  23.     }, false);
  24. }, false);
  25.  
  26. form.addEventListener("submit", function(event){
  27.     event.preventDefault();
  28.     if (envio){
  29.         this.submit();
  30.     }
  31.     else{
  32.         alert("Debe de ingresar otro correo electrónico");
  33.         input.value = "";
  34.         input.focus();
  35.     }
  36. }, false);

Codigo.css
Código CSS:
Ver original
  1. .valid{
  2.     background: green;
  3. }
  4. .invalid{
  5.     background: red;
  6. }
  7. .valid, .invalid{
  8.     color: white;
  9. }

comprobar.php
Código PHP:
$conexion mysqli_connect('localhost''root''''dbjeison');
if (!
$conexion) exit(mysqli_connect_error());
$correo mysqli_real_escape_string($conexionstrip_tags(trim($_POST['correo'])));
$consulta "SELECT correo FROM usuarios WHERE correo = '$correo'";
$resultados mysqli_query($conexion$consulta) or exit(mysqli_error());
echo 
mysqli_num_rows($resultados) ? 'yes' 'no'
registrar.php
Código PHP:
Ver original
  1. <html>
  2. <body>
  3. hola Pasar a registrar
  4. </body>
  5. </html>

Gracias por la paciencia y por enseñarme!