Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/02/2012, 12:56
nobo
 
Fecha de Ingreso: diciembre-2011
Mensajes: 151
Antigüedad: 12 años, 3 meses
Puntos: 1
Formulario de registro en php

Hola tengo un formulario creado por DW pero me gustaria hacerme el mio propio ya que creo que seria mas sencillo que entender el de DW...

Pongo el que tengo:

Código PHP:
Ver original
  1. <?php require_once('Connections/conexionzulo.php'); ?>
  2. <?php
  3. if (!function_exists("GetSQLValueString")) {
  4. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  5. {
  6.   if (PHP_VERSION < 6) {
  7.     $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  8.   }
  9.  
  10.   $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
  11.  
  12.   switch ($theType) {
  13.     case "text":
  14.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  15.       break;    
  16.     case "long":
  17.     case "int":
  18.       $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  19.       break;
  20.     case "double":
  21.       $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
  22.       break;
  23.     case "date":
  24.       $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  25.       break;
  26.     case "defined":
  27.       $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  28.       break;
  29.   }
  30.   return $theValue;
  31. }
  32. }
  33.  
  34. // *** Redirect if email exists
  35. $MM_flag="MM_insert";
  36. if (isset($_POST[$MM_flag])) {
  37.   $MM_dupKeyRedirect="alta_emailrepetido.php";
  38.   $loginUsername = $_POST['strEmail'];
  39.   $LoginRS__query = sprintf("SELECT * FROM tblusuario WHERE strEmail=%s",  GetSQLValueString($loginUsername, "text"));
  40.   mysql_select_db($database_conexionzulo, $conexionzulo);
  41.   $LoginRS=mysql_query($LoginRS__query, $conexionzulo) or die(mysql_error());
  42.   $loginFoundUser = mysql_num_rows($LoginRS);
  43.  
  44.   //if there is a row in the database, the email was found - can not add the requested username
  45.   if($loginFoundUser){
  46.     $MM_qsChar = "?";
  47.     //append the email to the redirect page
  48.     if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
  49.     $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
  50.     header ("Location: $MM_dupKeyRedirect");
  51.     exit;
  52.   }
  53. }
  54.  
  55. // *** Redirect if username exists
  56. $MM_flag="MM_insert";
  57. if (isset($_POST[$MM_flag])) {
  58.   $MM_dupKeyRedirect="alta_usuariorepetido.php";
  59.   $loginUsername = $_POST['strNombre'];
  60.   $LoginRS__query = sprintf("SELECT * FROM tblusuario WHERE strNombre=%s",  GetSQLValueString($loginUsername, "text"));
  61.   mysql_select_db($database_conexionzulo, $conexionzulo);
  62.   $LoginRS=mysql_query($LoginRS__query, $conexionzulo) or die(mysql_error());
  63.   $loginFoundUser = mysql_num_rows($LoginRS);
  64.  
  65.   //if there is a row in the database, the username was found - can not add the requested username
  66.   if($loginFoundUser){
  67.     $MM_qsChar = "?";
  68.     //append the username to the redirect page
  69.     if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
  70.     $MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar ."requsername=".$loginUsername;
  71.     header ("Location: $MM_dupKeyRedirect");
  72.     exit;
  73.   }
  74. }
  75.  
  76.  
  77. $editFormAction = $_SERVER['PHP_SELF'];
  78. if (isset($_SERVER['QUERY_STRING'])) {
  79.   $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  80. }
  81.  
  82. if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  83.   $insertSQL = sprintf("INSERT INTO tblusuario (strNombre, strEmail, intActivo, strPassword, strDireccion) VALUES (%s, %s, %s, %s, %s)",
  84.                        GetSQLValueString($_POST['strNombre'], "text"),
  85.                        GetSQLValueString($_POST['strEmail'], "text"),
  86.                        GetSQLValueString($_POST['intActivo'], "int"),
  87.                        GetSQLValueString($_POST['strPassword'], "text"),
  88.                        GetSQLValueString($_POST['strDireccion'], "text"));
  89.  
  90.   mysql_select_db($database_conexionzulo, $conexionzulo);
  91.   $Result1 = mysql_query($insertSQL, $conexionzulo) or die(mysql_error());
  92.  
  93.   $insertGoTo = "alta_ok.php";
  94.   if (isset($_SERVER['QUERY_STRING'])) {
  95.     $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
  96.     $insertGoTo .= $_SERVER['QUERY_STRING'];
  97.   }
  98.   header(sprintf("Location: %s", $insertGoTo));
  99. }
  100. ?>
  101. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  102. <html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/Principal.dwt.php" codeOutsideHTMLIsLocked="false" -->
  103. <head>
  104. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  105. <!-- InstanceBeginEditable name="doctitle" -->
  106. <title>El Zulo</title>
  107. <!-- InstanceEndEditable -->
  108. <!-- InstanceBeginEditable name="head" -->
  109. <script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
  110. <link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
  111. <!-- InstanceEndEditable -->
  112. <link href="estilo/principal.css" rel="stylesheet" type="text/css" />
  113. </head>
  114.  
  115. <body>
  116.  
  117. <div class="container">
  118.    
  119. <div class="header"><div class="headerinterior"><img src="images/banner1.jpg" alt="Tienda Zulo" width="980" height="180" /></div>
  120. </div>
  121.   <div class="subcontenedor">
  122.   <div class="sidebar1">
  123.    <?php include("includes/catalogo.php"); ?> <!-- Incluimos el catalogo que llama a la base de datos -->
  124.   <!-- end .sidebar1 --></div>
  125.   <div class="content">
  126.     <h1><!-- InstanceBeginEditable name="Titulo" -->Alta Usuario<!-- InstanceEndEditable --></h1>
  127.     <!-- InstanceBeginEditable name="Contenido" -->
  128.     <p>Formulario de Alta:   </p>
  129.     <p>&nbsp;</p>
  130.     <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  131.       <table align="center">
  132.         <tr valign="baseline">
  133.           <td nowrap="nowrap" align="right">Nombre:</td>
  134.           <td><span id="sprytextfield1">
  135.             <input type="text" name="strNombre" value="" size="32" />
  136.             <span class="textfieldRequiredMsg">Este campo es obligatorio.</span></span></td>
  137.         </tr>
  138.         <tr valign="baseline">
  139.           <td nowrap="nowrap" align="right">Email:</td>
  140.           <td><span id="sprytextfield2">
  141.           <input type="text" name="strEmail" value="" size="32" />
  142.           <span class="textfieldRequiredMsg">Este campo es obligatorio.</span><span class="textfieldInvalidFormatMsg">El Email no es correcto.</span></span></td>
  143.         </tr>
  144.  
  145.         <tr valign="baseline">
  146.           <td nowrap="nowrap" align="right">Contraseña:</td>
  147.           <td><span id="sprytextfield3">
  148.             <input type="password" name="strPassword" value="" size="32" />
  149.             <span class="textfieldRequiredMsg">Este campo es obligatorio.</span></span></td>
  150.         </tr>
  151.         <tr valign="baseline">
  152.           <td nowrap="nowrap" align="right">Direccion:</td>
  153.           <td><span id="sprytextfield4">
  154.             <input type="text" name="strDireccion" value="" size="32" />
  155.             <span class="textfieldRequiredMsg">Necesitamos la direccion para enviar el pedido.</span></span></td>
  156.         </tr>
  157.         <tr valign="baseline">
  158.           <td nowrap="nowrap" align="right">&nbsp;</td>
  159.           <td><input type="submit" value="Registrame!" /></td>
  160.         </tr>
  161.       </table>
  162.       <input type="hidden" name="intActivo" value="1" />
  163.       <input type="hidden" name="MM_insert" value="form1" />
  164.     </form>
  165.     <p>&nbsp;</p>
  166.     <script type="text/javascript">
  167. var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
  168. var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "email");
  169. var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");
  170. var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4");
  171.     </script>
  172.     <!-- InstanceEndEditable --><!-- end .content --></div>
  173.     <!-- end .subcontenedor --></div>
  174.    
  175.   <div class="footer">
  176.     <p>Pie de Pagina.</p>
  177.     <!-- end .footer --></div>
  178.   <!-- end .container --></div>
  179. </body>
  180. <!-- InstanceEnd --></html>

Me gustaria algo asi... Que se requieran los campos, me gustaria tener 2 campos para contraseña y email para repetirlos y comprobar que coinciden... Y lo mas importante me gustaria que comprobase si el nombre o el mail existe ya en la base de datos.. Y redireccionarlo a alguna pagina de error (como lo tengo puesto yo) o avisarlo en el mismo formulario. La cosa es que no se por donde empezar...

El form action puede ser la misma pagina donde este el formulario, no ?