PHP 表单不检查其他输入

Posted

技术标签:

【中文标题】PHP 表单不检查其他输入【英文标题】:PHP form does not check other inputs 【发布时间】: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 并检查两个字段是否都为空。只有当所有内容都正确填写时,它才会发送消息。 您尝试过什么解决问题的方法?你被困在哪里了? 【参考方案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");
    

?>

【讨论】:

以上是关于PHP 表单不检查其他输入的主要内容,如果未能解决你的问题,请参考以下文章

PHP 使用输入检查,简单验证码和错误报告进行表单处理

PHP将输入值发送到没有输入名称的表单

用 php 中的文件和其他输入处理发布的 formData 对象

使用 PHP 将 JSON 数据放入 html 表单输入复选框

制作“批准表格”页面的一些 php 帮助

PHP/HTML:输入后保存表单数据