PHP表单验证的问题

时间:2016-09-18 19:18:18

标签: php html forms validation slider

我的表单分为两页。使用名为Slick的JS滑块切换页面。在填写表单并单击第2页上的“发送”按钮后,验证表明我没有填写我的姓名。你可以在这个网站上看到这个:Rofordaward。只需填写提名表并推送即可。下面是HTML和PHP代码。

HTML



SNIPPET OF FORM (from nominate.html):

<form action="nominate.php" method="post">

  <div class="single-item slider">
    <div class="page1">
      <label class="row">
        <h2 class="headline">Your full name</h2>
        <input type="text" name="yourname" placeholder="Forename and surname"></input>
      </label>

      <label class="row email">
        <h2 class="headline">Your email address <p>Don't worry, we won't spam you or share your email address</p></h2>
        <input type="text" name="email" placeholder="example@rofordaward.co.uk"></input>

      </label>

      <label class="row">
        <h2 class="headline">Name of company</h2>
        <input type="text" name="companyname" placeholder="e.g. Roford"></input>
      </label>
    </div>

    <div class="page2">
      <label class="row reason">
        <h2 class="headline">Reason for nomination</h2>
        <textarea id="textarea" rows="6" cols="25" maxlength="1000" name="reason" placeholder="A brief evidence based summary"></textarea>
        <div id="text-area-wrap">
          <div id="textarea_feedback"></div>
        </div>
      </label>

      <div class="row button-wrap">
        <div class="column small-12">
          <input class="button" type="submit" value="Send it!">
        </div>
      </div>

    </div>

  </div>

</form>

   
&#13;
&#13;
&#13;

PHP         

    /* Check all form inputs using check_input function */
    $yourname = check_input($_POST['yourname'], "Enter your name");
    $email    = check_input($_POST['email']);
    $companyname    = check_input($_POST['companyname']);
    $reason = check_input($_POST['reason'], "Write your reason");

    /* If e-mail is not valid show error message */
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
        show_error("E-mail address not valid");
    }

    /*Message for the e-mail */
    $message = "New submission

    Name: $yourname
    E-mail: $email
    Company name: $companyname

    Reason:
    $reason

    End of message
    ";

    /* Send the message using mail() function */
    mail($myemail, $subject, $message);

    /* Redirect visitor to the thank you page */
    header('Location: thanks.htm');
    exit();

    /* Functions used */
    function check_input($data, $problem='')
    {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        if ($problem && strlen($data) == 0)
        {
            show_error($problem);
        }
        return $data;
    }

    function show_error($myError)
    {
    ?>
        <html>
        <body>

        <b>Please correct the following error:</b><br />
        <?php echo $myError; ?>

        </body>
        </html>
    <?php
    exit();
    }
    ?>

当我删除JS滑块的脚本时,表单将完全正常运行。

1 个答案:

答案 0 :(得分:0)

在您的PHP代码中,您$_POST['email']使用的<input type="text" name="email" />符合上一个HTML阻止代码的效果'email',因为您已将其设置为<label class="row email"> .... <input type="text" name="youremail" ....></input> </label>

另一方面,在您的第一个HTML代码块中:

name="youremail"

你设置$_POST['youremail'],这肯定会导致PHP代码导出错误的结果。您应该像PHP代码中的org.springframework.util.ClassUtils.resolveClassName一样使用它来使其正常工作。