【问题标题】:PHP form not finding empty fieldPHP表单找不到空字段
【发布时间】:2019-03-09 09:31:14
【问题描述】:

我一直在颠倒这个 php 表单,但我不知道为什么没有显示错误消息,即使所有字段都是空的,表单也会始终发送电子邮件。有我的代码:

<form name="contactform" method="post" action="">
  <input type="text" data-placeholder="<?php echo $name; ?>"name="name">
  <input type="text" data-placeholder="<?php echo $email; ?>"name="email">
  <textarea data-placeholder="<?php echo $message; ?>" name="message" rows="10"></textarea>
  <input type="submit" name="submit" class="btn btn-footer" value="ENVOYER" id="btnContact">
  <p class="text desktop"><?php echo $emailErr; ?></p>
</form>

<?php
  if(isset($_POST['submit'])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $emailErr = "";

    if (!isset($name) || $email=="" || $message=="" ) {
      $emailErr = "All fields are mendatory";
    } else {
      $mailTo = "test@gmail.com";
      mail($mailTo);
      $emailErr = "Email Send";
    }
 }
 ?>

【问题讨论】:

  • 不要使用isset()查看变量是否为空。像其他人一样使用$name == ""
  • 使用 !empty 代替它会完美工作
  • @sradha 但是如果他们的名字是0呢?
  • @FrankerZ 是的,如果我们输入 0 它将不起作用,我编辑了我的代码。谢谢你的建议。

标签: php forms email send


【解决方案1】:

isset() 在这种情况下将始终返回 true,因为变量已设置(为空字符串)。要检查变量是否为空,您应该使用您对 $email$message 所做的操作:

if ( $name == "" || $email == "" || $message == "" ) {

为了演示,您可以简单地运行以下命令:

$a = ""
var_dump(!isset($a)); // false
var_dump($a == ""); // true

【讨论】:

    【解决方案2】:

    表单提交后,如果字段名存在则isset() 将返回true。但是,如果您检查任何字段的空值,您可以使用 empty($fieldname)$fieldname == ""$fieldname == NULL

    试试这个:

    if(isset($_POST['submit'])) {
      $name = $_POST['name'];
      $email = $_POST['email'];
      $message = $_POST['message'];
      $emailErr = "";
    
      if (empty($name) || empty($email) || empty($message) ) {
        $emailErr = "All fields are mendatory";
      } else {
        $mailTo = "test@gmail.com";
        mail($mailTo);
        $emailErr = "Email Send";
      }
    }
    

    【讨论】:

      【解决方案3】:
      if (empty($name) || empty($email) || empty($message)) 
       {
           $emailErr = "All fields are mendatory";
       }
      

      你可以使用空的php函数或is_null

      【讨论】:

        【解决方案4】:

        请试试这个,

        我在我的本地系统中检查了它的工作正常,错误和成功消息会根据您的要求来

        <?php
          if(isset($_POST['submit'])) {
            $name = $_POST['name'];
            $email = $_POST['email'];
            $message = $_POST['message'];
            $emailErr = "";
        
            if (!isset($name) || $email=="" || $message=="" ) {
              $emailErr = "All fields are mendatory";
            } else {
              $mailTo = "test@gmail.com";
              mail($mailTo);
              $emailErr = "Email Send";
            }
         }
         ?>
         <form name="contactform" method="post" action="">
          <input type="text" data-placeholder="<?php echo $name; ?>"name="name">
          <input type="text" data-placeholder="<?php echo $email; ?>"name="email">
          <textarea data-placeholder="<?php echo $message; ?>" name="message" rows="10"></textarea>
          <input type="submit" name="submit" class="btn btn-footer" value="ENVOYER" id="btnContact">
          <p class="text desktop"><?php echo $emailErr; ?></p>
        </form>
        

        【讨论】:

        • 感谢它也对我有用。 必须在
          之前
        猜你喜欢
        • 2016-04-15
        • 2017-11-17
        • 2020-06-14
        • 2014-05-25
        • 2013-04-16
        • 1970-01-01
        • 1970-01-01
        • 2020-02-12
        • 1970-01-01
        相关资源
        最近更新 更多