html - Registration page. Printing out user errors in php -


i've created registration page whch checks errors , prints them out user. example if not have valid email address says invalid email address. @ top logic done lets webpage know if there errors later can register them without errors. in bottom html trying printout errors show next text field. have created errors reason print before user prints anything. code follows:

<form action="" method="post">                     <input type="hidden" name="action" value="register" />                     <?php if(@$_post['action'] == 'register') {                     }                     <input type="text" name="firstname" maxlength = "16" class="text-bars" placeholder="firstname"/>                     <input type="text" name="lastname" maxlength = "16" class="text-bars" placeholder="lastname"/><br /><br />                     <input type="text" name="username" maxlength = "16" class="text-bars" placeholder="username" />                     <?php if(user_exists($_post['username']) === true){                             echo "username exsits.";                             }else if(preg_match("/\\s/", $_post['username']) == true){                             echo 'your username must not contain spaces.';                             }                     ?>                     <br /><br />                     <input type="password" name="password" maxlength = "16" class="text-bars" placeholder="password" />                     <input type="password" name="re_password" maxlength = "16" class="text-bars" placeholder="re-enter password" />                              if($_post['password'] !== $_post['re_password']){                             echo "your passwords not match.";                             }else if(strlen($_post['password']) !== 0 && strlen($_post['password']) < 6){                             echo "your password must @ least 6 characters.";                             }                      ?>                     <br /><br />                     <input type="text" name="email" maxlength = "25" class="text-bars" placeholder="email" />                     <input type="text" name="re_email" maxlength = "25" class="text-bars" placeholder="re-enter email" />                     <?php                         if(filter_var($_post['email'], filter_validate_email) === false && $_post['email'] !== 0){                             echo "a valid email address required.";                         }else if(email_exists($_post['email']) === true){                             echo "email address in use.";                         } else if($_post['email'] !== $_post['re_email']){                             echo "your emails not match.";                         }                     ?>                     <br /><br />                     <input type="text" name="zipcode" maxlength = "7" class="text-bars" placeholder="zip code" /><br /><br />                      <input type="submit" value="submit" />           </form> 

the errors checks password above 6 characters long , email validate seem print right when page loads. solutions problem?

thanks everyone!

$errors = array();  if(empty($_post) === false){ $required_fields = array('firstname', 'lastname', 'username', 'password', 're_password', 'email', 're_email', 'zipcode'); foreach($_post $key=>$value){     if(empty($value) && in_array($key, $required_fields) === true){         $errors[] = 'all fields required';         break 1;     } }  if(empty($errors) === true){     if(user_exists($_post['username']) === true){         $errors[] = 'username exsits.';         }     if(preg_match("/\\s/", $_post['username']) == true){         $errors[] = 'your username must not contain spaces.';     }     if(strlen($_post['password']) < 6){         $errors[] = 'your password must @ least 6 characters.';     }     if($_post['password'] !== $_post['re_password']){         $errors[] = 'your passwords not match.';                             }     if(strlen($_post['password']) !== 0 && strlen($_post['password']) < 6){         $errors[] = 'your password must @ least 6 characters.';                             }     if(filter_var($_post['email'], filter_validate_email) === false){         $errors[]= 'a valid email address required.';     }     if(email_exists($_post['email']) === true){         $errors[] = 'email address in use.';     }      if($_post['email'] !== $_post['re_email']){         $errors[] = 'your emails not match.';     } } 

}

add input hidden code inside <form> element.

<input type="hidden" name="action" value="register" /> 

around php code, use following

<?php if(@$_post['action'] == 'register') {  } ?> 

this trigger php code when form submitted user.

this exact code need

<?php     if(@$_post['action'] == 'register') {         if(user_exists($_post['username']) === true){             $erroruser = "username exsits.";         }else if(preg_match("/\\s/", $_post['username']) == true){             $erroruser = 'your username must not contain spaces.';         }          if($_post['password'] !== $_post['re_password']){             $errorpassword = "your passwords not match.";         }else if(strlen($_post['password']) !== 0 && strlen($_post['password']) < 6){             $errorpassword = "your password must @ least 6 characters.";         }          if(filter_var($_post['email'], filter_validate_email) === false && $_post['email'] !== 0){             $erroremail = "a valid email address required.";         }else if(email_exists($_post['email']) === true){             $erroremail = "email address in use.";         } else if($_post['email'] !== $_post['re_email']){             $erroremail = "your emails not match.";         }     } ?>  <form action="" method="post">     <input type="hidden" name="action" value="register" />     <input type="text" name="firstname" maxlength = "16" class="text-bars" placeholder="firstname"/>     <input type="text" name="lastname" maxlength = "16" class="text-bars" placeholder="lastname"/>     <?=@$erroruser;?>     <br /><br />     <input type="text" name="username" maxlength = "16" class="text-bars" placeholder="username" />     <br /><br />     <input type="password" name="password" maxlength = "16" class="text-bars" placeholder="password" />     <input type="password" name="re_password" maxlength = "16" class="text-bars" placeholder="re-enter password" />     <?=@$errorpassword;?>     <br /><br />     <input type="text" name="email" maxlength = "25" class="text-bars" placeholder="email" />     <input type="text" name="re_email" maxlength = "25" class="text-bars" placeholder="re-enter email" />     <?@$erroremail;?>     <br /><br />     <input type="text" name="zipcode" maxlength = "7" class="text-bars" placeholder="zip code" /><br /><br />     <input type="submit" value="submit" /> </form> 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -