Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/06/2008, 13:05
Avatar de T4ke0veR
T4ke0veR
 
Fecha de Ingreso: agosto-2007
Ubicación: Quito - Ecuador
Mensajes: 1.720
Antigüedad: 17 años, 5 meses
Puntos: 28
[APORTE] Registro de usuario PHP y ajax

Bueno señores este formulario fue gracias a la colaboracion de personas q dia a dia entran a este foro, ya q aclarando mis dudas pude culminarlo.
Aun pueden pulirlo un poco mas!!

base de datos
CREATE TABLE `clientes` (
`id` int(11) NOT NULL auto_increment,
`usuario` text NOT NULL,
`pass` text NOT NULL,
`nombre` text NOT NULL,
`apellido` text NOT NULL,
`mail` text NOT NULL,
`cedula` text NOT NULL,
`telefono` text NOT NULL,
`fecha` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

primero hacemos la conexion a la base de datos
conect.php
Código PHP:
<?php

$host
="localhost";

$QQ="usuario";

$p="pass";

$db=mysql_connect($host,$QQ,$p)

or die (
"No pude conectarme a la base de datos");

mysql_select_db("nombre_de_la_base_de_datos")

or die (
"No puedo acceder a la base de datos del sistema");

?>
luego vamos al form
Código PHP:
<div id="log">
        <
div id="log_res">
        <!-- 
SPANNER -->
        </
div>
    </
div><div id="container">
                                        <
form method="post" id="form" action="register.php">
                                          <
table align="center" cellpadding="2" cellspacing="0">
                                            <
tr>
                                              <
td width="120" style="width:120px"><div align="right" class="textoResto">Nombre: </div></td>
                <
td width="172"><div align="left" class="string">
                  <
input name="nombre" type="text" class="input" id="nombre" value="" size="32" />
                  </
div>              </td>
              </
tr>
                                            <
tr>
                                              <
td class="textoResto" style="width:120px"><div align="right">Apellido: </div></td>
                <
td><div align="left">
                  <
input name="apellido" class="input" type="text" id="apellido" value="" size="32" />
                  </
div></td>
              </
tr>
                                            <
tr>
                                              <
td class="textoResto" style="width:120px"><div align="right">Usuario: </div></td>
                <
td><div align="left">
                  <
input name="usuario" type="text" class="input" id="usuario" value="" size="32" />
                  </
div>              </td>
              </
tr>
                                            <
tr>
                                              <
td class="textoResto" style="width:120px"><div align="right">Contraseña: </div></td>
                <
td><div align="left">
                  <
input name="pass" type="password" class="input" id="pass" value="" size="32" />
                  </
div></td>
              </
tr>
                                            <
tr>
                                              
                                              <
td class="textoResto" style="width:120px"><div align="right">Repetir Contraseña: </div></td>
                <
td><div align="left">
                  <
input name="re_pass" type="password" class="input" id="re_pass" value="" size="32" />
                  </
div></td>
              </
tr>
                                            <
tr>
                                              
                                              <
td class="textoResto" style="width:120px"><div align="right">E-Mail: </div></td>
                <
td><div align="left">
                  <
input name="mail" type="text" class="input" id="mail" value="" size="32" />
                  </
div></td>
              </
tr>
                                            <
tr>
                                              <
td style="width:120px"><div align="right" class="textoResto">C.I. / RIF.:</div></td>
                <
td><input name="cedula" type="text" class="input" id="cedula" value="" size="32" /></td>
              </
tr>
                                            <
tr>
                                              <
td style="width:120px"><div align="right" class="textoResto">Teléfono: </div></td>
                <
td><input name="telefono" type="text" class="input" id="telefono" value="" size="32" /></td>
              </
tr>
                                            <
tr>
                                              <
td style="width:120px"><div align="right" class="textoResto">Fecha: </div></td>
                <
td><div align="left">
                  <
input name="fecha" type="text" class="input" id="fecha" value="" size="25" />
                </
div></td>
              </
tr>
                                            <
tr>
                                              <
td colspan="2"><div align="right">
                                                <
input type="image" name="register" class="submit-btn" src="btn.gif" alt="submit" title="submit" />
                                                </
div></td>
              </
tr>
                                            </
table>
          </
form
en la misma página debemos incluir los javascript y un estilo css
Código PHP:
<script type="text/javascript" src="js/mootools.js"></script>
    <script type="text/javascript">
        window.addEvent('domready', function(){
                    $('registerForm').addEvent('submit', function(e) {
                        new Event(e).stop();
                        var log = $('log_res').empty().addClass('ajax-loading');
                        this.send({
                            update: log,
                            onComplete: function() {
                                log.removeClass('ajax-loading');
                            }
                        });
                    });
                });
    </script>
    <style type="text/css">

        body {
            font:0.7em Arial, helvetica, sens-serif;
            color:#567475;
        }
        .input {
            border: 1px solid #99b3b4;
            width: 170px;
            background: #e4ebeb;
            font: 11px verdana, sans-serif;
            color:#443;
            padding:3px;
            margin-bottom:4px;
            outline:none;
        }
        .input:focus {
            border:0px solid #567475;
            background: #e4ebeb;
        }
        .submit-btn {
            width: 54px;
            height: 20px;
            background: #743 url(submit.gif) no-repeat;
            outline: none;
        }
        .submit-btn:hover {
            background: #069 url(submit.gif) no-repeat 0 -20px;
        }
        div#container {
            border:0px solid #99b3b4;
            padding:15px;
            margin:auto;
            width:245px;
        }
        #log_res {
            height:auto;
            padding:10px;
            margin:10px auto 10px auto;
            width:270px;
        }
        #log_res p {
            margin:0;
            padding:4px 0 4px 0;
        }
        #log_res.ajax-loading
            {background: url(spinner.gif) no-repeat center;
        }
        .error {
            color:red;
            margin:0;
            padding:0;
        }

    </style> 
lugo leemos el archivo de verificacion de datos y la consulta a la db
register.php
Código PHP:
<?php
    
include('funciones.php');//las validaciones de los campos

    
if ($_POST['nombre']=='' || strlen($_POST['nombre'])<3)
    {
        
$errors[] = 'Nomnbre debe poseer al menos 3 letras.';
    }

    if (
$_POST['apellido']=='' || strlen($_POST['apellido'])<3)
    {
        
$errors[] = 'Apellido debe poseer al menos 3 letras.';
    }

    if (
$_POST['usuario']=='' || alpha_numeric($_POST['usuario'])==FALSE)
    {
        
$errors[] = 'Usuario debe contener valores alfa-numéricos';
    }

    if (
$_POST['pass']=='' || alpha_numeric($_POST['pass'])==FALSE)
    {
        
$errors[] = 'A password is required and must be alpha-numeric';
    }

    if (
$_POST['pass']!=$_POST['re_pass'])
    {
        
$errors[] = 'The two passwords must match';
    }

    if (
valid_email($_POST['mail'])==FALSE)
    {
        
$errors[] = 'Por favor, use una direccioón válida de E-Mail';
    }
    if (
$_POST['cedula']=='' || strlen($_POST['cedula'])<6)
    {
        
$errors[] = 'Cédula debe contener al menos 6 caracteres';
    }
    if (
$_POST['telefono']=='' || strlen($_POST['telefono'])<6)
    {
        
$errors[] = 'Telefono debe contener al menos 6 caracteres';
    }
    if (
$_POST['fecha']=='' || strlen($_POST['fecha'])<8)
    {
        
$errors[] = 'Fecha debe contener al menos 8 caracteres';
    }

    if(
is_array($errors))
    {
        echo 
'<p class="error"><b>Han ocurrido los siguientes errores</b></p>';
        while (list(
$key,$value) = each($errors))
        {

            echo 
'<span class="error">'.$value.'</span><br />';
        }
    }
    else {
    
//aqui colocas tu consulta o si lo vas a enciar via mail
         
include ('conect.php');

$nombre $_POST['nombre'];
$apellido $_POST['apellido'];
$pass $_POST['pass'];
$usuario $_POST['usuario'];
$mail $_POST['mail'];
$cedula $_POST['cedula'];
$telefono $_POST['telefono'];
$fecha $_POST['fecha'];


        
$query="INSERT INTO clientes SET

        nombre = '$nombre',

        apellido = '$apellido',

        usuario = '$usuario',
        
        pass = '$pass',
        
        mail = '$mail',
        
        cedula = '$cedula',
        
        telefono = '$telefono',
        
        fecha = '$fecha'"
;    

         
mysql_query($query) or die( "[ERROR] Query: $query, error: " mysql_error() );  



                
        echo 
'<p><b>Felicitaciones!</b></p>';
        echo 
'<span>Tu registro ha sido procesado con éxito. Ahora puedes ingresar usando tu usuario y contraseña. gracias por registrarte !</span>';
    }
?>
y por ultimo el archivo que define las funcion de verificacion
funciones.php
Código PHP:
<?php

    
function alpha_numeric($str)
    {
        return ( ! 
preg_match("/^([-a-z0-9])+$/i"$str)) ? FALSE TRUE;
    }

    function 
valid_email($str)
    {
        return ( ! 
preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix"$str)) ? FALSE TRUE;
    }

?>
solo les faltaria el moontools.js q pueden descargarselo d aqui
http://mad4milk.net

Gracias a quienes me ayudaron y a quienes se tomaron la molestia d leer.
Y gracias en general a los q hoy en dia hacen d este foro algo util "los usuarios"


Habran muchas personas q podran mejorarlo!!

Espero les sea util a muchos.

Saludos
__________________
Diseño gráfico, Web, imagen corporativa, publicidad ...