PHP - phpform

 
Vista:

phpform

Publicado por joker (3 intervenciones) el 16/02/2015 02:02:49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
//Definimos la codificación de la cabecera.
header('Content-Type: text/html; charset=utf-8');
//Importamos el archivo con las validaciones.
require_once 'funciones/validaciones.php';
//Guarda los valores de los campos en variables, siempre y cuando se haya enviado el formulario, sino se guardará null.
$nombre = isset($_POST['nombre']) ? $_POST['nombre'] : null;
$apellido1 = isset($_POST['apellido1']) ? $_POST['apellido1'] : null;
$apellido2 = isset($_POST['apellido2']) ? $_POST['apellido2'] : null;
$dni = isset($_POST['dni']) ? $_POST['dni'] : null;
$edad = isset($_POST['edad']) ? $_POST['edad'] : null;
$sexo = isset($_POST['sexo']) ? $_POST['sexo'] : null;
$nivel = isset($_POST['nivel']) ? $_POST['nivel'] : null;
//Este array guardará los errores de validación que surjan.
$errores = array();
 
//pruebas* if(isset($_POST['enviar'])){
 
//Pregunta si está llegando una petición por POST, lo que significa que el usuario envió el formulario.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
 
   //Comprobar validaciones
   if (!validaRequerido($nombre )) {
      $errores[] = 'Por favor comprueba que el campo Nombre no este vacio.';
   }
    if (!validaRequerido($apellido1 )) {
      $errores[] = 'Por favor comprueba que el campo Apellido1 no este vacio.';
   }
    if (!validaRequerido($dni )) {
      $errores[] = 'Por favor comprueba que has escrito tu DNI';
   }
    if (!validardni($dni )) {
      $errores[] = 'Por favor comprueba que has escrito correctamente tu DNI';
   }
   //Valida la edad con un rango de 3 a 130 años.
   $opciones_edad = array(
      'options' => array(
         //Definimos el rango de edad entre 3 a 130.
         'min_range' => 3,
         'max_range' => 130
      )
   );
   if (!validarEntero($edad, $opciones_edad)) {
      $errores[] = 'Comprueba que tu edad se encuentra entre 3 y 130 años.';
   }
	if (!validaRequerido($sexo )) {
      $errores[] = 'Por favor comprueba que has seleccionado tu Sexo.';
   }
    if (!validaRequerido($nivel )) {
      $errores[] = 'Por favor comprueba que has seleccionado el Nivel.';
   }
   //Verifica si ha encontrado errores y de no haber redirige a la página con el mensaje de que pasó la validación.
   if(!$errores){
      header('Location: comprobar.php');
	  exit;
   }
}
?>
<!DOCTYPE>
<html>
<head>
	<title>Formulario de Jesús</title>
	 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
    <?php if ($errores): ?>
       <ul style="color: #f00;">
          <?php foreach ($errores as $error): ?>
             <li> <?php echo $error ?> </li>
          <?php endforeach; ?>
       </ul>
    <?php endif; ?>
    <form method="post" action="index.php">
	<h2 align="center"> Ficha de Futbol </h2>
	<form name="registro" method="post" action="index.php">
		<table width="350" border="1" align="center" cellpadding="4" cellpacing="0" bgcolor="#00ffcc">
			<tr>
				<td width"57%" align="right">
					Nombre (*)
				</td>
				<td coldspan="2">
						<input type="text" name="nombre" size="25" maxlength="25" value="<?php echo $nombre ?>" />
				</td>
			</tr>
 
			<tr>
				<td width"57%" height="47%"  align="right">
					Apellido 1 (*)
				</td>
				<td height="17" coldspan="2">
					<input type="text" name="apellido1" size="25" maxlength="25" value="<?php echo $apellido1 ?>" />
				</td>
			</tr>
 
			<tr>
				<td width"57%"  align="right">
					Apellido 2
				</td>
				<td height="17" coldspan="2">
					<input type="text" name="apellido2" size="25" maxlength="25" align="center" value="<?php echo $apellido2 ?>" />
				</td>
			</tr>
 
 
			<tr>
				<td width"57%"  align="right">
					DNI (*)
				</td>
				<td height="17" coldspan="2">
					<input type="text" name="dni" size="25" maxlength="25" value="<?php echo $dni ?>" />
				</td>
			</tr>
 
 
			<tr>
				<td width"57%"  align="right">
					Edad (*)
				</td>
				<td height="17" coldspan="2">
					<input type="text" name="edad" size="3" maxlength="3" value="<?php echo $edad ?>" />
				</td>
			</tr>
 
			<tr>
				<td width"57%"  align="right">
					sexo (*)
				</td>
				<td>
					<table border="0">
						<tr>
							<td height="2" width="86%">
								<input type="radio" name="sexo" value="Masculino" value="<?php echo $sexo ?>" /> Hombre
								<input type="radio" name="sexo" value="Femenino" value="<?php echo $sexo ?>" /> Mujer
							</td>
 
						</tr>
					</table>
				</td>
			</tr>
 
			<tr>
   				<td width"57%" height="47%" align="right">
   					Nivel (*)
   				</td>
   				<td height="2">
   					<select name="nivel" value="<?php echo $nivel ?>" />
            			<option value="Pre-benjamin">Pre-Benjamin
            			<option value="Benjamin">Benjamin
            			<option value="Alevin">Alevin
           	 			<option value="Infantil">Infantil
            			<option value="Cadete">Cadete
            			<option value="Juvenil">Juvenil
            			<option value="Senior">Senior
            		</select>
            	</td>
            <tr >
   				<td colspan="2" align="center">
					<input type="submit" name="enviar" VALUE="enviar">
				</td>
			</tr>
	</table>
</br>
<p>* Por favor recuerda que todos los campos con (*) son campos obligatorios.
</body>
 
</html>


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<html>
 
<body>
	<head align="center"><h1>Registro de Jes&uacute;s Jaramillo Sarmiento</h1></head>
<?php
header('Content-Type: text/html; charset=utf-8');
$nombre = isset($_POST['nombre']) ? $_POST['nombre'] : null;{
echo "Tu nombre es" '".$nombre."' "<br />";
		echo "Primer Apellido: ".$_POST['apellido1']."<br />";
		echo "Segundo Apellido: ".$_POST['apellido2']."<br />";
		echo "Tu sexo es ".$_POST['sexo']. "<br />";
		echo  "y tienes un nivel de ".$_POST['nivel']. "<br />";
		}
?>
<p id="cancelar"></p>
<table align="left" width="40%" border="0" cellspacing="0" cellpadding="10">
<tr>
	<td align="center" colspan="2">
	 ¿Es correcta esta informacion?
	</td>
</tr>
<tr>
	<td align="center">
	<button onclick="confirmar()">Si</button>
		<script>
			function confirmar() {
				if (confirm("Los datos se han guardado correctamente.") == true) {
					x = window.open('index.php')
				} else {
					x = "Has cancelado el REGISTRO!!!";
				}
				document.getElementById("cancelar").innerHTML = x;
			}
		</script>
	</td>
	<td align="center">
		<a href="javascript:history.back()"><input type="button" value="No" /> </a>
	</td>
</table>
 
</body>
</html>
 
<?php
 function validaRequerido($valor){
    if(trim($valor) == ''){
       return false;
    }else{
       return true;
    }
 }
 function validarEntero($valor, $opciones=null){
    if(filter_var($valor, FILTER_VALIDATE_INT, $opciones) === FALSE){
       return false;
    }else{
       return true;
    }
 }
 
function validardni($valor) {
	function numerodni($numdni) {return substr("TRWAGMYFPDXBNJZSQVHLCKE",$numdni%23,1);}//función para asignar la letra de control
	$patron="^([0-9]{1,8})([T,R,W,A,G,M,Y,F,P,D,X,B,N,J,Z,S,Q,V,H,L,C,K,E]{1})$";//patrón que controla que haya entre 1 y 8 números y una letra de las de la lista; además carga en el array los datos numéricos y la letra por separado
	if (ereg($patron, $valor, $regs) && $regs[2]==numerodni($regs[1])) {
		return true;
	}else {
		return false;
	}
}
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
-1
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

phpform

Publicado por xve (6935 intervenciones) el 16/02/2015 12:47:14
Pero... que problema tienes?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar