Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/01/2004, 16:04
SLandgren
 
Fecha de Ingreso: enero-2004
Mensajes: 3
Antigüedad: 20 años, 3 meses
Puntos: 0
Formulario - Validar

Estoy aprendiendo php, y estoy haciendo un formulario, y quiero validar los campos, y luego mandarlo a una base de datos, (mysql) ya logre hacer q cuando no es valido no envie a la base de datos, pero tengo dudas con los mensajes de error que coloco para los formularios no validos...
coloque al lado del texfield una variable $error la cual la hago "" o "Error ingresar mail nuevamente" pero m problema es q al entrar a la pagina por primera vez ya me aparece el mensaje de error, siendo q esta asignacion esta dentro de una funcion, lo q pienso yo es q como no tiene valor inicial, agarra ese...nose, necesito ayuda, pq ya me quebre la cabeza tratando de arreglarlo............ aca dejo el codigo......

<?php
//Connection statement
require_once('Connections/db2.php');

//Aditional Functions
require_once('includes/functions.inc.php');
//global $error;
//$errorm=$error;
//if(!$i==1){$error="";}
//$error="";
function Validar(){
//global $error;
//$error="";
//$i=1;
if(comprobar_email($_POST['email'])){
$error="";
return true;}
else {
$emilio=$_POST['email'];
$error="El email ".$emilio." es incorrecto";
return false;}
}

function comprobar_email($email){
$mail_correcto = 0;
//compruebo unas cosas primeras
if ((strlen($email) >= 6) && (substr_count($email,"@") == 1) && (substr($email,0,1) != "@") && (substr($email,strlen($email)-1,1) != "@")){
if ((!strstr($email,"'")) && (!strstr($email,"\"")) && (!strstr($email,"\\")) && (!strstr($email,"\$")) && (!strstr($email," "))) {
//miro si tiene caracter .
if (substr_count($email,".")>= 1){
//obtengo la terminacion del dominio
$term_dom = substr(strrchr ($email, '.'),1);
//compruebo que la terminación del dominio sea correcta
if (strlen($term_dom)>1 && strlen($term_dom)<5 && (!strstr($term_dom,"@")) ){
//compruebo que lo de antes del dominio sea correcto
$antes_dom = substr($email,0,strlen($email) - strlen($term_dom) - 1);
$caracter_ult = substr($antes_dom,strlen($antes_dom)-1,1);
if ($caracter_ult != "@" && $caracter_ult != "."){
$mail_correcto = 1;
}
}
}
}
}
if ($mail_correcto==1)
return true;
else
return false;
}


if (Validar()){

// build the form action
$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'] . (isset($HTTP_SERVER_VARS['QUERY_STRING']) ? "?" . $HTTP_SERVER_VARS['QUERY_STRING'] : "");

if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO formulario (apellido, email) VALUES (%s, %s)",
GetSQLValueString($HTTP_POST_VARS['nombre'], "text"),
GetSQLValueString($HTTP_POST_VARS['email'], "text"));

$Result1 = $db2->Execute($insertSQL) or die($db2->ErrorMsg());
}
}

// begin Recordset
$query_rs_chica = "SELECT apellido, email FROM formulario";
$rs_chica = $db2->SelectLimit($query_rs_chica) or die($db2->ErrorMsg());
$totalRows_rs_chica = $rs_chica->RecordCount();
// end Recordset

//PHP ADODB document - made with PHAkt 2.6.0
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="<?php echo $editFormAction; ?>" name="form1" method="POST">

<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Nombre</td>
<td>
<input name="nombre" type="text" id="nombre"></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" type="text" id="email"><?php if(!Validar()){echo $error;}?></td>
</tr>
<tr>
<td>
<input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>

<input type="hidden" name="MM_insert" value="form1">
</form>
</body>
</html>
<?php
$rs_chica->Close();
?>