php表单验证必填字段

时间:2014-07-16 22:11:41

标签: php

我有一个PHP表单,我正在尝试制作必需的字段,但我似乎无法让它正常工作,我知道它缺少的东西。我只需要将表单需要$ name $ bank $ email和$ phone。

<?php
    /* Set e-mail recipient */
    $myemail  = "email@email.com";


    /* Check all form inputs using check_input function */
    $name = check_input($_POST['name'], "Enter your name");
    $bank    = check_input($_POST['bank'], "Enter your name");
    $email    = check_input($_POST['email']);
    $phone  = check_input($_POST['phone']);
    $headphones = check_input($_POST['headphones']);
    $subject = "Allied Affiliated Funding - Bose Landing Page Form Submission";
    /* If e-mail is not valid show error message */
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
    {
        show_error("E-mail address not valid");
    }

    /* Let's prepare the message for the e-mail */
    $message = "Hello!



    Your contact form has been submitted by:

    Name: $name
    Bank: $bank
    E-mail: $email
    Phone: $phone
    Head Phones: $headphones

    End of message
    ";

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

    /* Redirect visitor to the thank you page */
    if($_POST['headphones'] == Yes){ 
        header("Location: landing1/thank-you-bose.html"); 
    } 
    else { 
        header("Location: landing1/thank-you.html"); 
    }  
    exit();

    /* Functions we 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();
    }
    ?>

我已经看了几个关于如何完成这个的不同教程但是没有一个解决方案似乎有效。

1 个答案:

答案 0 :(得分:0)

我认为你的问题就在这一行

   if($_POST['headphones'] == Yes){ 

需要

   if($_POST['headphones'] == "Yes"){ 

您没有名为Yes的变量,所以我想您要检查耳机的内容是否为“是”。