簡體   English   中英

php表單驗證必填字段

[英]php form validation required fields

我有一個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();
    }
    ?>

我看過一些有關如何完成此操作的教程,但似乎沒有一種解決方案有效。

我認為您的問題出在這條線上

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

它必須是

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

您沒有名為Yes的變量,所以我認為您想檢查耳機的內容是否為“ yes”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM