Hola Comunidad aquí diseñando un estilo de formulario.. me saca un error ya teniendo dos variables definidas en código php cuando lo corro en el servidor local me sale diciendo que las variables
$sexo;
$hobbie;
No se encuentran definidas quisiera que me ayudaran adjunto el formulario !
----------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<HTML>
<?php$nombre="";$clave="";$sexo="";$hobbie="";$msgNombre="";$msgClave="";$msgSexo="";$msgHobbie="";$claseNombre="";$claseClave="";$claseSexo="";$claseHobbie="";if ($_POST){$nombre = $_POST["nombre"];$clave = $_POST["clave"];$sexo = $_POST["sexo"];$hobbie = $_POST["hobbie"];if ($nombre == ""){ $msgNombre = " ¡..Atencion..! Nombre Vacio * "; $claseNombre="error"; } else{ if(is_numeric($nombre)){$msgNombre=" ¡..Atencion..! El Campo Nombre No Puede Ser Numerico * ";$claseNombre="error"; }}if ($clave == ""){$msgClave = " ¡..Atencion..! Contraseña Vacia * ";$claseClave="error"; } if ($sexo == ""){$msgSexo = " ¡..Atencion..! Escoja Un Sexo * ";$claseSexo="error";} if ($hobbie == ""){$msgHobbie = " ¡..Atencion..! Escoja Un Hobbie * ";$claseHobbie="error"; } if($claseNombre=="" && $claseClave==""){ echo "<center><h2> ::..OK... Sus Datos Fueron Ingresados Al Servidor ..:: <h2></center>"; echo "<center><h2> ::..OK... Muchas Gracias ..:: <h2></center>"; } if($claseSexo=="" && $claseHobbie==""){ echo "<center><h2> ::..OK... Sus Datos Fueron Ingresados Al Servidor ..:: <h2></center>"; echo "<center><h2> ::..OK... Muchas Gracias ..:: <h2></center>"; }}//finaliza el if de post ?>
<head>
<title>Formulario</title>
<style>
.error{
color: orange;
border: solid 5px red;
}
.msg{
color: white;
font-size: 8px;
font-size: 9cm
}
</style>
</head>
<body>
<form name="form1" action="send.php" method="POST">
<center><fieldset>
<legend>send.php</legend>
<table border="2">
<div class="<?php echo $claseNombre; ?>
">
<tr><td>Nombres</td>
<td><input type="text" name="nombre" value="<?php echo $nombre; ?>
"></td>
<span class=".msg"> <?php echo $msgNombre;?>
</span>
</tr>
</div>
<div class="<?php echo $claseClave; ?>
">
<tr><td>Contraseña</td>
<td><input type="password" name="clave" maxlength="4" size="4" value="<?php echo $clave; ?>
" ></td>
<span class=".msg"><?php echo $msgClave;?>
</span>
</tr>
</div>
<div class="<?php echo $claseSexo; ?>
">
<tr><td>Escoja sexo</td>
<td><input type="radio" name="sexo" value="H" value="<?php echo $sexo; ?>
" >Hombre
<input type="radio" name="sexo" value="M" value="<?php echo $sexo; ?>
">Mujer
<span class=".msg"><?php echo $msgSexo;?>
</span>
</tr>
</div>
<div class="<?php echo $claseHobbie; ?>
">
<tr><td>Escoja Hobbie(s)</td>
<td><input type="checkbox"name="hobbie" value="1" value="<?php echo $hobbie; ?>
" >Deportes
<input type="checkbox" name="hobbie" value="1" value="<?php echo $hobbie; ?>
">Música
<input type="checkbox" name="hobbie" value="1" value="<?php echo $hobbie; ?>
">Internet
<input type="checkbox" name="hobbie" value="1" value="<?php echo $hobbie; ?>
">Lectura
<span class=".msg"><?php echo $msgHobbie;?>
</span>
</td>
</tr>
</div>
<tr><td>Escoja Profesión</td>
<td><select name="prof">
<option value="1">Ingeniería</option>
<option value="2" selected>Medicina</option>
<option value="3" >Analista de Sistemas</option>
<option value="4">Administrador de Empresas</option>
</select>
</td>
</tr>
<tr><td>Escriba su comentario ...</td>
<td><textarea name="coment" rows="6" cols="30">
</textarea>
</td>
</tr>
</table>
<input type="reset" value="Limpiar">
<select name="guarde">
<option value="si">Si guarde en archivo</option>
<option value="no" selected>No guarde en archivo</option>
</select>
<input type="submit" value="Validar">
</fieldset>
</form>
</body>
</HTML>
Si vas a permitir que el usuario escoja uno o varias opciones de hobbies veo más lógico tener una variable para cada hobbie, de forma que puedas saber si está marcado o no. Por eso usaríamos $hobbie1 para saber si el primer hobbie está marcado, $hobbie2 para saber si el segundo está marcado y así sucesivamente. Aquí está el código, además he quitado la repetición de value
<!DOCTYPE html>
<HTML>
<?php$nombre="";$clave="";$sexo="";$hobbie1="false";$hobbie2="false";$hobbie3="false";$hobbie4="false";$msgNombre="";$msgClave="";$msgSexo="";$msgHobbie="";$claseNombre="";$claseClave="";$claseSexo="";$claseHobbie="";if ($_POST){$nombre = $_POST["nombre"];$clave = $_POST["clave"];$sexo = $_POST["sexo"];$hobbie1 = $_POST["hobbie1"];$hobbie2 = $_POST["hobbie2"];$hobbie3 = $_POST["hobbie3"];$hobbie4 = $_POST["hobbie4"];if ($nombre == ""){ $msgNombre = " ¡..Atencion..! Nombre Vacio * "; $claseNombre="error"; } else{ if(is_numeric($nombre)){$msgNombre=" ¡..Atencion..! El Campo Nombre No Puede Ser Numerico * ";$claseNombre="error"; }}if ($clave == ""){$msgClave = " ¡..Atencion..! Contraseña Vacia * ";$claseClave="error"; } if ($sexo == ""){$msgSexo = " ¡..Atencion..! Escoja Un Sexo * ";$claseSexo="error";} if ($hobbie1 == "" && $hobbie2=="" && $hobbie3=="" && $hobbie4==""){$msgHobbie = " ¡..Atencion..! Escoja Un Hobbie * ";$claseHobbie="error"; } if($claseNombre=="" && $claseClave=="" && $claseSexo=="" && $claseHobbie==""){ echo "<center><h2> ::..OK... Sus Datos Fueron Ingresados Al Servidor ..:: <h2></center>"; echo "<center><h2> ::..OK... Muchas Gracias ..:: <h2></center>"; } }//finaliza el if de post ?>
<head>
<title>Formulario</title>
<meta charset="utf-8">
<style>
.error{
color: orange;
border: solid 5px red;
}
.msg{
color: white;
font-size: 8px;
font-size: 9cm
}
</style>
</head>
<body>
<form name="form1" action="send.php" method="POST">
<center><fieldset>
<legend>send.php</legend>
<table border="2">
<div class="<?php echo $claseNombre; ?>
">
<tr><td>Nombres</td>
<td><input type="text" name="nombre" value="<?php echo $nombre; ?>
"></td>
<span class=".msg"> <?php echo $msgNombre;?>
</span>
</tr>
</div>
<div class="<?php echo $claseClave; ?>
">
<tr><td>Contraseña</td>
<td><input type="password" name="clave" maxlength="4" size="4" value="<?php echo $clave; ?>
" ></td>
<span class=".msg"><?php echo $msgClave;?>
</span>
</tr>
</div>
<div class="<?php echo $claseSexo; ?>
">
<tr><td>Escoja sexo</td>
<td><input type="radio" name="sexo" value="H" >Hombre
<input type="radio" name="sexo" value="M" >Mujer
<span class=".msg"><?php echo $msgSexo;?>
</span>
</tr>
</div>
<div class="<?php echo $claseHobbie; ?>
">
<tr><td>Escoja Hobbie(s)</td>
<td><input type="checkbox"name="hobbie1" value="true" >Deportes
<input type="checkbox" name="hobbie2" value="true" >Música
<input type="checkbox" name="hobbie3" value="true" >Internet
<input type="checkbox" name="hobbie4" value="true" >Lectura
<span class=".msg"><?php echo $msgHobbie;?>
</span>
</td>
</tr>
</div>
<tr><td>Escoja Profesión</td>
<td><select name="prof">
<option value="1">Ingeniería</option>
<option value="2" selected>Medicina</option>
<option value="3" >Analista de Sistemas</option>
<option value="4">Administrador de Empresas</option>
</select>
</td>
</tr>
<tr><td>Escriba su comentario ...</td>
<td><textarea name="coment" rows="6" cols="30">
</textarea>
</td>
</tr>
</table>
<input type="reset" value="Limpiar">
<select name="guarde">
<option value="si">Si guarde en archivo</option>
<option value="no" selected>No guarde en archivo</option>
</select>
<input type="submit" value="Validar">
</fieldset>
</form>
</body>
</HTML>
Aquí te dejo más o menos indicado cómo pienso que podría ser. Con este código creo que no quedan variables indefinidas simplemente porque he quitado las variables, que al final creo que tampoco eran imprescindibles. A mí parece que me va funcionando pero tendrás que revisarlo tú a ver qué te parece. Saludos.
<!DOCTYPE html>
<HTML>
<?phpif ($_POST["guarde"]) {if (!$_POST["nombre"]){ $msgNombre = " ¡..Atencion..! Nombre Vacio * "; $claseNombre="error"; } else{ if(is_numeric($nombre)){$msgNombre=" ¡..Atencion..! El Campo Nombre No Puede Ser Numerico * ";$claseNombre="error"; }}if (!$_POST["clave"]){$msgClave = " ¡..Atencion..! Contraseña Vacia * ";$claseClave="error"; } if (!$_POST["sexo"] ){$msgSexo = " ¡..Atencion..! Escoja Un Sexo * ";$claseSexo="error";} if (!$_POST["hobbie1"] && !$_POST["hobbie2"] && !$_POST["hobbie3"] && !$_POST["hobbie4"]){$msgHobbie = " ¡..Atencion..! Escoja Un Hobbie * ";$claseHobbie="error"; } if($claseNombre=="" && $claseClave=="" && $claseSexo=="" && $claseHobbie==""){ echo "<center><h2> ::..OK... Sus Datos Fueron Ingresados Al Servidor ..:: <h2></center>"; echo "<center><h2> ::..OK... Muchas Gracias ..:: <h2></center>"; } } ?>
<head>
<title>Formulario</title>
<meta charset="utf-8">
<style>
.error{
color: orange;
border: solid 5px red;
}
.msg{
color: white;
font-size: 8px;
font-size: 9cm
}
</style>
</head>
<body>
<form name="form1" action="testJM031013borrar.php" method="POST">
<center><fieldset>
<legend>send.php</legend>
<table border="2">
<div class="<?php echo $claseNombre; ?>
">
<tr><td>Nombres</td>
<td><input type="text" name="nombre" value="<?php echo $_POST["nombre"]; ?>
"></td>
<span class=".msg"> <?php echo $msgNombre;?>
</span>
</tr>
</div>
<div class="<?php echo $claseClave; ?>
">
<tr><td>Contraseña</td>
<td><input type="password" name="clave" maxlength="4" size="4" value="<?php echo $_POST["clave"]; ?>
"></td>
<span class=".msg"><?php echo $msgClave;?>
</span>
</tr>
</div>
<div class="<?php echo $claseSexo; ?>
">
<tr><td>Escoja sexo</td>
<td><input type="radio" name="sexo" <?php if ($_POST["sexo"]=="H") {echo 'value="H" checked';} else {echo 'value="H"';} ?>
>Hombre
<input type="radio" name="sexo" <?php if ($_POST["sexo"]=="M") {echo 'value="M" checked';} else {echo 'value="M"';} ?>
>Mujer
<span class=".msg"><?php echo $msgSexo;?>
</span>
</tr>
</div>
<div class="<?php echo $claseHobbie; ?>
">
<tr><td>Escoja Hobbie(s)</td>
<td><input type="checkbox"name="hobbie1" <?php if ($_POST["hobbie1"]=="true") {echo 'value="true" checked';} else {echo 'value="true"';} ?>
>Deportes
<input type="checkbox" name="hobbie2" <?php if ($_POST["hobbie2"]=="true") {echo 'value="true" checked';} else {echo 'value="true"';} ?>
>Música
<input type="checkbox" name="hobbie3" <?php if ($_POST["hobbie3"]=="true") {echo 'value="true" checked';} else {echo 'value="true"';} ?>
>Internet
<input type="checkbox" name="hobbie4" <?php if ($_POST["hobbie4"]=="true") {echo 'value="true" checked';} else {echo 'value="true"';} ?>
>Lectura
<span class=".msg"><?php echo $msgHobbie;?>
</span>
</td>
</tr>
</div>
<tr><td>Escoja Profesión</td>
<td><select name="prof">
<option value="1">Ingeniería</option>
<option value="2" selected>Medicina</option>
<option value="3" >Analista de Sistemas</option>
<option value="4">Administrador de Empresas</option>
</select>
</td>
</tr>
<tr><td>Escriba su comentario ...</td>
<td><textarea name="coment" rows="6" cols="30">
</textarea>
</td>
</tr>
</table>
<input type="reset" value="Limpiar">
<select name="guarde">
<option value="si">Si guarde en archivo</option>
<option value="no" selected>No guarde en archivo</option>
</select>
<input type="submit" value="Validar">
</fieldset>
</form>
</body>
</HTML>