简体   繁体   English

PHP表单验证问题

[英]Problems with PHP form validation

My form is spread across two pages. 我的表格分布在两页中。 Pages are turned using the JS slider called Slick. 使用称为Slick的JS滑块打开页面。 After filling in the form and clicking the send button on page 2, the validation says I didn't fill in my name. 填写表格并单击第2页上的“发送”按钮后,验证提示我没有填写姓名。 You can see this for yourself on this website: Rofordaward . 您可以在以下网站上亲自查看此内容: Rofordaward Just complete the nomination form and push send. 只需填写提名表格并推送即可。 HTML and PHP code below. 下面的HTML和PHP代码。

HTML 的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="eg 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> 

PHP 的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();
    }
    ?>

When I remove the scripts for the JS slider, the form becomes fully functional. 当我删除JS滑块的脚本时,该表单将完全起作用。

In your PHP Code, You are using $_POST['email'] which suits good according to code on your last HTML Block when <input type="text" name="email" /> as you have set it to 'email' 在您的PHP代码中,您正在使用$_POST['email'] ,根据您在上一个HTML块上的代码,当<input type="text" name="email" />设置为'email'时,此代码'email'

On other hand, in your First HTML Code Block: 另一方面,在您的第一个HTML代码块中:

<label class="row email">
        ....
        <input type="text" name="youremail" ....></input>
 </label>

You set name="youremail" , which is definitely going to derive wrong result with your PHP Code. 您设置name="youremail" ,这肯定会使您的PHP代码得出错误的结果。 You should be using it like $_POST['youremail'] in your PHP Code to get it working. 您应该像在PHP代码中那样使用$_POST['youremail']来使其正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM