簡體   English   中英

PHP 表單驗證在提交時不起作用

[英]PHP form validation not working on submit

我有一個鏈接到 php 文件的 HTML 表單。 當我點擊提交表單發送時,我收到一封電子郵件。 但是,如果表單為空並且我單擊提交,它仍會重定向到主頁。

表單 HTML:

     <form class="form-horizontal" role="form" method="post" action="contact.php"> 
                <div class="form-group"> 
                    <label for="name" class="col-sm-2 control-label">Name</label> 
                    <div class="col-sm-10"> 
                        <input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name"> 
                                        </div> 
                </div> 
                <div class="form-group"> 
                    <label for="telephone" class="col-sm-2 control-label">Phone</label> 
                    <div class="col-sm-10"> 
                        <input type="telephone" class="form-control" id="telephone" name="telephone" placeholder="Phone Number" > 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <label for="email" class="col-sm-2 control-label">Email</label> 
                    <div class="col-sm-10"> 
                        <input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" > 
                    </div> 
                </div> 
                <div class="form-group"> 

                  <label for="dropdown" class="col-sm-2 control-label">Select One:</label>
                  <div class="col-sm-10">
                  <select name="select" class="form-control" id="dropdown">
                    <option>Driver</option>
                    <option>Advertiser</option>
                    </select>
                </div>
                </div>
                <div class="form-group"> 
                    <label for="message" class="col-sm-2 control-label">Message</label> 
                    <div class="col-sm-10"> 
                        <textarea class="form-control" rows="4" name="message"></textarea> 
                    </div> 
                </div>
                <div class="form-group"> 
                    <div class="col-sm-10 col-sm-offset-2"> 
                        <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary"> 
                    </div> 
                </div> 
                <div class="form-group"> 
                    <div class="col-sm-10 col-sm-offset-2"> 

                    </div> 
                </div>
            </form>           

聯系方式

    <?php
    if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $telephone= $_POST['telephone'];
            $select = $_POST['select'];
    $message = $_POST['message'];
    $from = 'Contact Form'; 
    $to = 'myemail@gmail.com'; 
    $subject = 'Message from Contact Form';

    $body ="From: $name\n E-Mail: $email\n Telephone: $telephone\n Select: $select\n Message:\n $message";

    // Check if name has been entered
    if (!$_POST['name']) {
        $errName = 'Please enter your name';
    }

    // Check if email has been entered and is valid
    if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $errEmail = 'Please enter a valid email address';
    }

    //Check if message has been entered
    if (!$_POST['message']) {
        $errMessage = 'Please enter your message';
    }

    // If there are no errors, send the email
    if (!$errName && !$errEmail && !$errMessage && !$errTel) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
        } else {
            $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
        }
    }
    {
    //  To redirect form on a particular page
    header("Location:http://www.mywebsite.co");
    }
    }
    ?>

知道為什么我的驗證不起作用嗎?

謝謝你。

你的代碼中有兩個額外的括號,這個:

 {
 //  To redirect form on a particular page
 header("Location:http://www.mywebsite.co");
 }

應該只是:

 //  To redirect form on a particular page
    header("Location:http://www.mywebsite.co");

您正在關閉 if 塊並打開一個空塊。

您可能想重新檢查這部分:

{
//  To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}

此外,您還應該對 POST 數據使用修剪功能。

聯系方式

if (isset($_POST["submit"])) {
  $name      = trim($_POST['name']);
  $email     = trim($_POST['email']);
  $telephone = trim($_POST['telephone']);


  /* Other code */

  if (!$errName && !$errEmail && !$errMessage && !$errTel) {
      if (mail ($to, $subject, $body, $from)) {
        $result='<div class="alert alert-success">Thank You! I will be in touch</div>';
      } else {
        $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
      }
  } else {
   //  To redirect form on a particular page
   header("Location:http://www.mywebsite.co");
  }

暫無
暫無

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

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