如何在php中验证表格 [英] How to validate form in php

查看:49
本文介绍了如何在php中验证表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对此有一个解决方案,我是php的菜鸟,我无法弄清楚.

I'm hoping there is a solution for this, I'm a noob in php, I couldn't figure it out.

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

I got some help from here, to validate the html form in php. It is working ok..but I found that after I hit the submit button & php shows the errors, there is a input field, asking the user to answer the math Ques..when I enter the answer, then hit submit button again, it just says mail successfully sent, but other fields are still empty. Any ideas how to fix it. Also, the php value in the answerbox input tags, is not saving the text in box, it disappears.

感谢您的时间!

<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-->

推荐答案

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

As you are a newcomer to PHP I would give you some tips for a beginner as I can.

首先是要输出HTML代码,然后再处理PHP.这可能不是一个好习惯(解决问题的方法与此紧密相关).您正在做的就是一直显示表单,没有限制,事后通过PHP进行检查/验证.一个好的做法是在任何形式的php出现之前都不要输出html代码(除了示例之外,还有header()函数之类的其他问题).

First is that you are outputting an HTML code and processing PHP afterwards. This might not be a good practice (and solution to your problem is quite connected to this). What you are doing is showing a form all the time, without restrictions and checking/validating via PHP afterwards. A good practice is to never output html code before any kind of php goes (there are other issues like of header() function besides your example).

您希望做的是:

<?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>

我要给您的另一提示是,在构建类似的静态页面时(我的意思是结构化程度较低,面向对象的对象较少.)一个好的做法是将您正在回显/打印"的内容存储在其中变量并将其很好地输出.

Another thing I would give you as a tip is that when building static pages like that (I mean less structured, less object oriented, you know..) a good practice is to store what your are 'echoing / printing' in variables and output them nicely where applicable.

所以不是

<?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>

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

I hope these humble opinions will help you at least maybe to solve your problem. Good luck

这篇关于如何在php中验证表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆