如何在php中验证表单

时间:2013-08-30 02:58:40

标签: php html forms

我希望有一个解决方案,我是php中的菜鸟,我无法理解。

我从这里得到了一些帮助,以验证php中的html表单。它工作正常..但我发现在我点击提交按钮& php显示错误,有一个输入字段,要求用户回答数学问题。当我输入答案时,再次点击提交按钮,它只是说邮件已成功发送,但其他字段仍为空。任何想法如何解决它。另外,answerbox输入标签中的php值不会将文本保存在框中,它会消失。

谢谢你的时间!

<div id="contact">
  <h1 class="heading1"> Contact:</h1>
  <form name="contact" action="formtest.php" method="post">
    <label for="YourName">Your Name:</label>
    <input type="text" name="name" class="required" value="<?= isset($_POST['name']) ? $_POST['name'] : '' ?>" />

    <label for="YourEmail">Your Email:</label>
    <input type="text" name="email" class="required" value="<?= isset($_POST['email']) ? $_POST['email'] : '' ?>"/>

    <label for="Subject">Subject:</label>
    <input type="text" name="subject" class="required" value="<?= isset($_POST['subject']) ? $_POST['subject'] : '' ?>" />

    <label for="YourMessage">Your Message:</label>
    <textarea  name="message" class="required"><?= isset($_POST['message']) ? $_POST['message'] : '' ?></textarea>
    <p class="c3">10 + 5 =<input type="text" name="answerbox" id="answerbox" "<?= isset($_POST['answerbox']) ? $_POST['answerbox'] : '' ?>"/></p>

    <fieldset>
      <input type="submit" name="submit" id="submit" value="Send" class="required"/>
      <input type="reset" id="reset" value="Reset"/>    
    </fieldset>

    <?php
      if(isset($_POST['submit'])){
        $name = trim($_POST["name"]);
        $email = trim($_POST["email"]);
        $subject = trim($_POST["subject"]);
        $message = trim($_POST["message"]);
        $answerbox = trim($_POST["answerbox"]);
        if(empty($_POST['name'])){
          print "<div class='formerrors'><li>Please type your name.</li></div>";
        }
        else {
          if (ctype_alpha($name) === false) {
            print "<div class='formerrors'><li>Your name only should be in letters!</li></div>";
          }
        }

        if(empty($_POST['email'])){
          print "<div class='formerrors'><li>You've forgot to type your email address.</li></div>";
        } else{
          if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
            print "<div class='formerrors'><li>Your email is not valid, please check.</li></div>";
          }
        }

        if(empty($_POST['subject'])){
          print "<div class='formerrors'><li>Please type a subject.</li></div>";
        }

        if(empty($_POST['message'])){
          print "<div class='formerrors'><li>You've forgot to type your message.</li></div>";
        }

        if(empty($_POST['answerbox'])){
          print "<div class='formerrors'><li>Please answer the math question.</li></div>";
        }
        else {
          if($answerbox != 15){
              print"<div class='formerrors'><li>Answer is not correct.</li></div>";
          }
          else{
            $headers =  'From: '.$email. "\r\n" .
            'Reply-To: '.$email . "\r\n" .
            'X-Mailer: PHP/' . phpversion();
             mail('me@mymail.me',$subject,$message,$headers);
            print "<div class='formerrors'><li>mail succesuffully sent</li></div>";
          }
        }
      }
    ?>
  </form>
</div><!--contact-->

1 个答案:

答案 0 :(得分:2)

由于您是PHP的新手,我会尽可能为初学者提供一些提示。

首先,您要输出HTML代码并在之后处理PHP。这可能不是一个好习惯(解决问题的方法与此非常相关)。你正在做的是一直显示一个表单,没有限制,之后通过PHP检查/验证。一个好的做法是在任何类型的PHP之前都不输出html代码(除了你的例子之外还有header()函数之类的其他问题。)

你宁愿做的是:

<?php
  if (isset($_POST['submit'])) {
  // validations here
  // if (......) { echo 'problem 1...'; }
  // elseif (.....) { echo 'problem 2...'; }
  // else (.....} {
     // No problems so we can send it
     // mail(.....);
  //   }
  }

// done with PHP now show HTML
?>
<form>....</form>

我会给你一个小贴士的另一件事是,当构建这样的静态页面时(我的意思是结构更少,面向对象更少,你知道......)一个好的做法是存储你的'回声/打印'变量并在适用的地方很好地输出它们。

所以而不是

<?php
  if (empty($user)) { echo 'Username is empty!'; } // Directly echoing (perhaps in not a right place, maybe even before <!DOCTYPE><html> declaration!!
?>

你会选择

<?php
   $errors = '';
   if (empty($user)) { $errors .= '<br/>Error1: Provide an username'; }
   elseif (empty($name)) { $errors .= '<br/>Error2: Provide a name'; }
   else { /*.....*/ } 
   // Since we have our errors variable with errors (or empty one) we can check for it perhaps in HTML context
?>
<form>
  <?php if (!empty($errors)) { ?>
    <div style='color:red;'>There are some errors! :(</div>
    <?php echo $errors; ?>
    </div>
  <?php } ?>
</form>

我希望这些拙见可以帮助你至少解决你的问题。祝你好运