验证错误时似乎无法保留表格输入

时间:2019-03-26 10:17:47

标签: php html

我刚刚研究了Traversy Media PHP crashcourse thing,并试图将其实现到我自己的项目中。现在,我基本上是在验证数据,当我点击Submit时,所有输入到字段中的信息都会重置并为空白。在链接的视频中,他谈到了如何使用HTML中的值来解决此问题,到目前为止,它仍然有效。唯一的问题是textarea总是空白。我不知道为什么它对其他所有东西都起作用,但对那个特定元素却无效。我错过了什么?

<?php 
  // Message Vars
  $msg = '';
  $msgClass = '';
  // Check for Submit
  if(filter_has_var(INPUT_POST, 'submit')){
    //  get form data
    $name = htmlspecialchars($_POST['name']);
    $cname = htmlspecialchars($_POST['cname']);
    $email = htmlspecialchars($_POST['email']);
    $message = htmlspecialchars($_POST['message']);

    // check required fields
    if(!empty($email) && !empty($name) && !empty($message)){
        // passed
        //check emails
        if(filter_var($email, FILTER_VALIDATE_EMAIL)=== false){
          // failed 
          $msg = 'Please use a valid email';
          $msgClass = 'error-fail'; 
        } else {
          //Passed
          echo'passed';
        }
      } else {
        // Fail
        $msg = 'Please fill in all the required fields';
        $msgClass = 'error-fail'; 
      }
  }
?> 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Ravenfell</title>
    <link rel="stylesheet" href="./css/main.css" />
    <link rel="stylesheet" href="./css/contact.css" />
  </head>
  <body>
  <section id="sec5" class="lt-bg dk-txt desktop tablet">
      <div class="content-wrapper flex-container">
        <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" class="form-container">
          <div class="form-head">
            <h2>Connect with us</h2>
            <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Exercitationem, expedita.</p>
          </div>
          <!-- Fullname -->
          <p class="col1">Full Name:</p>
          <input type="text" name="name" placeholder="Your Name" class="col2" value="<?php echo isset($_POST['name']) ? $name : ''; ?>">
          <!-- Company Name -->
          <p class="col1">Company Name <span class="optional">(optional)</span>:</p>
          <input type="text" placeholder="Company Name" name="cname" class="col2" value="<?php echo isset($_POST['cname']) ? $cname : ''; ?>">
          <!-- email -->
          <p class="col1">Email:</p>
          <input type="email" name="email" placeholder="example@company.com" class="col2" value="<?php echo isset($_POST['email']) ? $email : ''; ?>">
          <!-- Message -->
          <p class="col1">Message:</p>
          <textarea name="message" cols="30" rows="10" placeholder="Message" value="<?php echo isset($_POST['message']) ? $message : ''; ?>"></textarea>
          <!-- Button and Errors -->
          <?php if($msg != ''): ?>
            <div class="alert <?php echo $msgClass ?>"><?php echo $msg ?></div>
          <?php endif; ?>
          <button type="submit" name="submit" class="submit-btn">Submit &#8594;</button>
        </form>
      </div>
    </section>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

textarea没有价值。更改您的文本区域,如下所示:

<textarea name="message" cols="30" rows="10" placeholder="Message"><?php echo isset($_POST['message']) ? $message : ''; ?></textarea>