Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/03/2010, 16:22
marcos46g
 
Fecha de Ingreso: julio-2009
Mensajes: 311
Antigüedad: 14 años, 9 meses
Puntos: 0
Pregunta validando formulario

Solamente necesito que me ayuden a entender porque cuando intento validar un formulario me ocurren cosas incoherentes.
Es decir la primera vez que al detectar un error o varios hago que aparezca nuevamente el formulario con los datos que ingreso y marcandole los errores.
El formulario se completa bien con los datos que envio en un link con todas las variables pero intentando prever que el usuario cometa nuevos errores intento hacer el proceso varias veces seguidas y ahi empiezan los errores.
Algo que no logro entender porque si unos minutos antes y con el mismo codigo php logro mandar el valor de la variable y ubicarla en el campo correspondiente
Luego de varios intentos seguidos se "pierde" el valor de algunos campos o los deja en blanco cuando se que el valor llego en el link pero no lo ubica correctamente. Incluso pude ver como por ejemplo despues de un par de veces de ir y venir una variable que traia un "no tiene " termina llenando el campo con un "no ti"
Voya pegar el codigo de validacion y partes aisladas del formulario pues es largo y no me permite pegarlo totalmente .
Pegare algunas partes para que vean como cargo los valores a ver si es mejorable el metodo que uso
Toda ayuda me vendra muy bien



Código PHP:
<?
session_start
();
include 
'db.php';
$idAviso$_POST[idAviso];
print_r ($_POST)."<BR><BR>";
$superficie=Trim($_POST['superficie']);
$adicional=Trim($_POST['adicional']);
$telefono2=Trim($_POST['telefono2']);
$telefono=Trim($_POST['telefono']);
//validando campos

         
if (empty($_POST['valor']) or !is_numeric($_POST['valor']))
         {
$error1="1";}else {$error1="2"; }


          if (empty(
$_POST['departamento'])or empty($_POST['zona']))
         {
$error7="1";}else {$error7="2"; }


         if(empty(
$_POST['superficie'])or !is_numeric($superficie))
        {
$error3="1";}else {$error3="2"; }


        if(empty(
$_POST['telefono']) && empty($_POST['telefono2']))
        { 
$error4="1";}else {$error4="2"; }
        

          if (!empty( 
$_POST[departamento])) {
          if (
$_POST[departamento]==1){$departamento="Montevideo";}
          if (
$_POST[departamento]==2){$departamento="Canelones";}
          if (
$_POST[departamento]==3){$departamento="Colonia";}
          }

         if (
$error1=="1" OR $error3=="1" OR $error4=="1" OR $error7=="1")
         {

         
$ruta="ModifAvisoA.php?idAviso=$idAviso && superficie=".$superficie." && dormitorios=$_REQUEST[dormitorios]&& banos=$_REQUEST[banos]&& portero=$_REQUEST[portero]&& ascensor=$_REQUEST[ascensor]&& garage=$_REQUEST[garage] && calefaccion=$_REQUEST[calefaccion]&& alarma=$_REQUEST[alarma]
         && jardin=$_REQUEST[jardin]&& camara=$_REQUEST[camara] && servicio=$_REQUEST[servicio] && terraza=$_REQUEST[terraza]&& piso=$_REQUEST[piso] && antiguedad=$_REQUEST[antiguedad]&& valor=$_REQUEST[valor]&& fpago=$_REQUEST[fpago]&& telefono=$_REQUEST[telefono] && telefono2="
.$telefono2."
         && horarioC=$_REQUEST[horarioC]&& departamento=$_REQUEST[departamento] zona=$_REQUEST[zona] && estadoP=$_REQUEST[estadoP] && adicional=$_REQUEST[adicional] && error1=$error1 && error3=$error3 && error4=$error4 && error7=$error7"
;

          
         
header("Location:$ruta");
          exit();
          }
         else
         {

          
         
$consultaSQL="Update avisos2 SET superficie='".$superficie."',
         dormitorios='"
.$_POST[dormitorios]."',
         banos='"
.$_POST[banos]."',
         portero='"
.$_POST[portero]."',
         ascensor='"
.$_POST[ascensor]."',
         garage='"
.$_POST[garage]."',
         calefaccion='"
.$_POST[calefaccion]."',
         alarma='"
.$_POST[alarma]."',
         jardin='"
.$_POST[jardin]."',
         camara='"
.$_POST[camara]."',
         servicio='"
.$_POST[servicio]."',
         terraza='"
.$_POST[terraza]."',
         piso='"
.$_POST[piso]."',
         antiguedad='"
.$_POST[antiguedad]."',
         valor='"
.$_POST[valor]."',
         fpago='"
.$_POST[fpago]."',
         telefono='"
.$telefono."',
         telefono2='"
.$telefono2."',
         horarioC='"
.$_POST[horarioC]."',
         departamento='"
.$departamento."',
         zona='"
.$_POST[zona]."',
         estadoP='"
.$_POST[estadoP]."',
         adicional='"
.$adicional."'
         WHERE idAviso='"
.$_POST[idAviso]."'";
         
        
// echo $consultaSQL."<Br>";
         //exit();

         
mysql_db_query($database$consultaSQL$conexion)or die (mysql_error());

         
          
$ruta2="location:MostAviso2.php?idAviso=$idAviso";
          
header("$ruta2");
          }

 
?>
partes del formulario
<script language=JavaScript>
function validar(){

if(document.getElementById('valor').value == ''){
alert("Es necesario que ingrese el VALOR DE LA PROPIEDAD.");
return false;
}

}
</script>

<script language=JavaScript>
//verifica que sean números los caracteres ingresados por el usuario
function validarSiNumero(numero){
if (!/^([0-9])*$/.test(numero))
alert("Ingresar CARACTERES NUMERICOS ,sin puntos ni ningún otro tipo de símbolos.");
}
</script>
<select name="banos" size="1" id="label6">
<option >
<? if(!empty($_GET["banos"])&& $_GET["banos"]<>"1")
{ echo $_GET["banos"];}
?>
</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
</select>
</span></label></td>

<td bgcolor="#0066CC"><select name="piso">
<option><? if(!empty($_GET["piso"] )&& $_GET["piso"]<>"Planta Baja") { echo $_GET["piso"];} ?> </option>
<option>Planta Baja</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<select></td>

<input name="telefono" type="text" id="telefono" onchange="validarSiNumero(this.value);" value="<? if(!empty($_GET["telefono"])){ echo $_GET["telefono"];}?>" size="15"/>

<input name="valor" type="text" id="cantidad" onChange="validarSiNumero(this.value);" value="<? echo $_GET["valor"];?>" size="15"/>

<select name="horarioC">
<? if(!empty($_GET["horarioC"])&& $_GET["horarioC"]<>"de mañana"){ echo $_GET["horarioC"];}?>
<option>de ma&ntilde;ana</option>
<option>de tarde</option>
<option>despues de las 20hrs</option>
<option>fines de semana</option>
</select>

<? if(!empty($_GET["departamento"])&& $_GET["departamento"]<>0){ echo $_GET["departamento"];} else {echo "Seleccione opción";}?>
</option>
<option value="1">Montevideo </option>
<option value="2">Canelones </option>
<option value="3">Colonia </option>
</select >
</span></td>
</tr>
<tr>
<th height="23" bgcolor="#336699" scope="row"><div align="left"><span class="Estilo11">Barrio / Ciudad o Localidad </span></div></th>
<td bgcolor="#336699"><span class="Estilo5">
<select name="zona" >
<option value="-">
<? if(!empty($_GET["zona"])){ echo $_GET["zona"];} else {echo "-";}?>
</option>
</select>

<textarea name="adicional" cols="55" rows="2" id="textarea"><? if(!empty($_GET["adicional"])){ echo $_GET["adicional"];}?>
</textarea>
<span class="Estilo3"><span class="Estilo2">
<input type="hidden" name="idAviso"value="<? if(!empty($_GET["idAviso"])){ echo $_GET["idAviso"];}?>" />
</span></span></div></th>

<input type="submit" name="Submit" value="Guardar Cambios" id="Submit" />

<?
if ($_GET["error3"]==1 )
{
$mensaje3=" SUPERFICIE es un campo numérico, no ingrese puntos, guiones, espacios ni otros símbolos.<br>Ingrese un valor aproximado si no dispone en este momento del dato exacto." ;
echo $mensaje3; }

?>
</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
</tr>
<tr>
<td height="201">&nbsp;</td>
</tr>
<tr>
<td height="62">&nbsp;</td>
</tr>
<tr>
<td height="42">&nbsp;</td>
</tr>
<tr>
<td height="43">&nbsp;</td>
</tr>
<tr>
<td height="43"><strong>
<?
if ($_GET["error1"]==1 )
{
$mensaje1="Ingrese el VALOR de la propiedad" ;
echo $mensaje1;
}

?>
</strong></td>
</tr>
<tr>
<td height="43"><strong>
<?

if ($_GET["error4"]==1)
{ $mensaje4="Ingrese un teléfono para ser contactado" ;
echo $mensaje4;}

?>
</strong></td>
</tr>
<tr>
<td height="36">&nbsp;</td>
</tr>
<tr>
<td height="36"><strong>
<?

if ($_GET["error7"]==1)
{ $mensaje4="Seleccione el DEPARTAMENTO y ZONA donde se encuentra la propiedad" ;
echo $mensaje4;}

?>