【问题标题】:PHP form does not check other inputsPHP 表单不检查其他输入
【发布时间】:2021-12-01 00:16:41
【问题描述】:

我已经制作了一个联系表格,但是 PHP 只检查“消息”,如果已填写,则忽略所有其他检查。如果未填写所有其他检查也可以。 有人可以帮我修复我的代码吗?谢谢。

<?php
$fullName = $customerEmail =  $message = '';
$errors = array('fullName' => '', 'customerEmail' => '', 'message' => '');

if (isset($_POST['submit'])) {
    $myEmail = "xxxxxxxxxxxxx";
    $timeStamp = date("dd/M/YY HH:i:s");
    $body = "";

    $fullName = $_POST['fullName'];
    $customerEmail = $_POST['customerEmail'];
    $chosenSubject = $_POST['subject'];
    $message = $_POST['message'];

    $body .= "From: " . $fullName . " at $timeStamp" . "\r\n";
    $body .= "Email: " . $customerEmail . "\r\n";
    $body .= "Message: " . $message . "\r\n";

    if (empty($fullName)) {
        $errors['fullName'] = "Full name is required. <br>";
    } else {
        if (!preg_match("/^([a-zA-Z' ]+)$/", $fullName)) {
            $errors['fullName'] = "Your name must be a valid name.";
        }
        
    }

    if (empty($customerEmail)) {
        $errors['customerEmail'] = "An email is required. <br>";
    } else {
        if (!filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
            $errors['customerEmail'] = "Email must be a valid email address.";
        }
    }

    if (empty($message)) {
        $errors['message'] = "A message is required.";
    } 
    
    else {
        mail($myEmail, $chosenSubject, $body);
        header("Location: public/mail_sent.php");
    }
}
?>

【问题讨论】:

  • 确保其他字段的名称也正确添加,或者在您的问题中也显示表单
  • 啊,现在实际上已修复它,通过将最后一个 else 更改为 if 并检查两个字段是否都为空。只有当所有内容都正确填写时,它才会发送消息。
  • 您尝试过什么解决问题的方法?你被困在哪里了?

标签: php forms


【解决方案1】:

你的最后一个else应该改为IF,检查是否有任何字段不为空,然后发送消息。

if(!empty($fullName) && !empty($customerEmail) && !empty($message)) {

    mail($myEmail, $chosenSubject, $body);

    header("Location: public/mail_sent.php");
}

【讨论】:

    【解决方案2】:

    Last else 更改为对所有字段进行 IF 检查,并且只有当它们都不为空时才会发送。

    <?php
    $fullName = $customerEmail =  $message = '';
    $errors = array('fullName' => '', 'customerEmail' => '', 'message' => '');
    
    if (isset($_POST['submit'])) {
        $myEmail = "danysko5@gmail.com";
        $timeStamp = date("dd/M/YY HH:i:s");
        $body = "";
    
        $fullName = $_POST['fullName'];
        $customerEmail = $_POST['customerEmail'];
        $chosenSubject = $_POST['subject'];
        $message = $_POST['message'];
    
        $body .= "From: " . $fullName . " at $timeStamp" . "\r\n";
        $body .= "Email: " . $customerEmail . "\r\n";
        $body .= "Message: " . $message . "\r\n";
    
        if (empty($fullName)) {
            $errors['fullName'] = "Full name is required. <br>";
        } else {
            if (!preg_match("/^([a-zA-Z' ]+)$/", $fullName)) {
                $errors['fullName'] = "Your name must be a valid name.";
            }
            
        }
    
        if (empty($customerEmail)) {
            $errors['customerEmail'] = "An email is required. <br>";
        } else {
            if (!filter_var($customerEmail, FILTER_VALIDATE_EMAIL)) {
                $errors['customerEmail'] = "Email must be a valid email address.";
            }
        }
    
        if (empty($message)) {
            $errors['message'] = "A message is required.";
        } 
        
        if(!empty($fullName) && !empty($customerEmail) && !empty($message)) {
            mail($myEmail, $chosenSubject, $body);
            header("Location: public/mail_sent.php");
        }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 2014-06-27
      相关资源
      最近更新 更多