Validate Empty / Radio Fields - Php -


<?php  $name = $_post["name"]; $email = $_post["email"]; $gender = $_post["gen"]; $age = $_post["age"]; $comments = $_post["comments"];  if(empty($_post["name"])){ echo "empty name"; } if(empty($_post["email"])){ echo "empty email"; }  /// if(empty($_post["gen"])){ echo "empty gender"; } if(empty($_post["comments"])){ echo "empty comments"; }  if(!isset($_post['submit'])) {  ?>  <html> <head> <title>lab6 : p1</title> </head>  <body> <fieldset> <legend><h4>enter information in fields below</h4></legend> <form name="info" method="post" action="<?php echo $php_self;?>" method="post"> <strong>name:</strong> <input type="text" name="name" id="name" /><br> <strong>email:</strong> <input type="text" name="email" id="email" /><br> <br> <strong>gender</strong><br> <input type="radio" name="gen" value="male">male</input><br> <input type="radio" name="gen" value="female">female</input><br> <br> <select name="age"> <option value="under 30">under 30</option><br> <option value="between 30 , 60">between 30 , 60</option><br> <option value="60+">60+</option> </select> <br> comments: <textarea name="comments" cols="20" rows="5"> </textarea> </fieldset> <input type="submit" name="submit" value="submit information" /> </form> </body> </html> <? }   else {  echo "thank you, ".$name." comments: " . "<strong>" . $comments . "</strong>"; echo "<br>we reply at:" . "<em>" . $email . "</em>"; }  ?> 

i need validate fields: name, email, comments empty strings (not allowed) , not allow unselected radio buttons gender , age selection.

can help

<?php  // re-fill fields $name = ""; $email = ""; $gender = ""; $age = ""; $comments = "";  // re-select radio button , select option $mchecked = ""; $fchecked = ""; $selectminus_30=""; $select30_to_60=""; $select60_plus="";  // display errors $error = "";  $done=false;  if (isset($_post["name"]) && isset($_post["email"]) && isset($_post["age"])){     if($_post["name"]==""){     $error = "empty name <br/>";     }      if($_post["email"]==""){     $error = $error . "empty mail <br/>";     }      if(!isset($_post["gen"])){     $error = $error . "empty gender <br/>";     }     else{         $gender = $_post["gen"];         if ($gender == "male"){             $mchecked = "checked";         }         else if ($gender == "female"){             $fchecked = "checked";         }     }      if($_post["comments"]==""){     $error = $error . "error: empty comments <br/>";     }       $name = $_post["name"];     $email = $_post["email"];     $comments = $_post["comments"];      $age = $_post["age"];     if ($age == "under 30"){         $selectminus_30 = "selected";     }     else if ($age == "between 30 , 60"){         $select30_to_60 = "selected";     }     else if ($age == "60+"){         $select60_plus = "selected";     }      if ($error==""){         $done=true;     } } ?>  <html> <head> <title>lab6 : p1</title> </head>  <body>  <?php if (!$done){ ?>     <fieldset>     <legend><h4>enter information in fields below</h4></legend>     <p class="error" style="color:red;"><?php echo $error;?></p>     <form name="info" method="post" action="<?php echo $_server["php_self"];?>" method="post">     <strong>name:</strong> <input type="text" name="name" id="name" value="<?php echo $name; ?>" /><br/>     <strong>email:</strong> <input type="text" name="email" id="email" value="<?php echo $email; ?>" /><br/>     <br/>     <strong>gender</strong><br/>     <input type="radio" name="gen" value="male" <?php echo $mchecked;?>>male</input><br/>     <input type="radio" name="gen" value="female" <?php echo $fchecked;?>>female</input><br/>     <br/>     <select name="age" value="<?php echo $age;?>">     <option value="under 30" <?php echo $selectminus_30;?>>under 30</option><br/>     <option value="between 30 , 60" <?php echo $select30_to_60;?>>between 30 , 60</option><br/>     <option value="60+" <?php echo $select60_plus;?>>60+</option>     </select>     <br/>     comments: <textarea name="comments" cols="20" rows="5"><?php echo $comments; ?></textarea>     </fieldset>     <input type="submit" name="submit" value="submit information" />     </form> <?php }else{     echo "thank you, ".$name." comments: " . "<strong>" . $comments . "</strong>";     echo "<br/>we reply at:" . "<em>" . $email . "</em>";  } ?>  </body> </html> 

here did proper validation, , re-fill form when submit empty field.


p.s. thank marc b , didn't realize first post aweful.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -