php - Keep form data inside input fields after form error checking -


i have small form, im doing simple error checking php , looks notice when users submit form, after error checking happens data removed fields.

here have

<?php if($show=="true"):?> <input name="fname" type="text" value="<?php if(isset($_post['name']){echo $_post['name';]})?>"><?php echo $errorname; ?>  <input name="email" type="text" value="<?php if(isset($_post['name']){echo $_post['name';]})?>"><?php echo $erroremail; ?> <input type="submit" value="submit"> <?php else: ?> <h2>your message sent</h2>   <?php endif;?> <?php     if(empty($_post['name'])){          $show="true";          $errorname="please enter name";      }      elseif(empty($_post['email'])){         $show=true;         $erroremail="please end email";      }else         $show=false;         //send data email;      ?> 

if want check if fields empty (which weird), use post values on last submission. after that, use empty(), if there indeed, errors, put them inside array (gather them) , print them after submission. consider example:

<?php $errors = array();  if(isset($_post['submit'])) {     $fname = trim($_post['fname']);     $email = trim($_post['email']);      if(empty($fname)) {         $errors['fname'] = 'please enter name';     }     if(empty($email)) {         $errors['email'] = 'please enter email';     } } ?>  <form method="post" action="index.php">     name: <input type="text" name="fname" value="<?php echo isset($_post['fname']) ? $_post['fname'] : ''; ?>" /> <span style="color: red;"><?php echo isset($errors['fname']) ? $errors['fname'] : ''; ?></span><br/>     email: <input type="text" name="email" value="<?php echo isset($_post['email']) ? $_post['email'] : ''; ?>" /> <span style="color: red;"><?php echo isset($errors['email']) ? $errors['email'] : ''; ?></span><br/>     <input type="submit" name="submit" value="submit" /> </form>  <?php if(isset($_post['submit']) && empty($errors)): ?>     <h2>your message sent.</h2> <?php endif; ?> 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -