当我添加$ name时,PHP联系人表单不起作用

时间:2019-01-30 03:49:23

标签: php

我的联系表格有一个奇怪的问题。由于某种原因,如果我的代码中没有$ name标记,则我的代码可以正常工作。这一定是我想念的简单事情!这是我的代码:

<?php

    $error = ""; $successMessage = "";

    if ($_POST) {
        
        if (!$_POST["email"]) {
            
            $error .= "An email address is required.<br>";
            
        }
        
        if (!$_POST["content"]) {
            
            $error .= "The content field is required.<br>";
            
        }
        
        if (!$_POST["subject"]) {
            
            $error .= "The subject is required.<br>";
            
        }
        
        if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
            
            $error .= "The email address is invalid.<br>";
            
        }
      
        if (!$_POST["name"]) {
            
            $error .= "Full name is required.<br>";
            
        }
        
        if ($error != "") {
            
            $error = '<div class="alert alert-danger" role="alert"><p>There were error(s) in your form:</p>' . $error . '</div>';
            
        } else {
          
          	$name = $_POST['name'];
            
            $emailTo = "name@example.com";
            
            $subject = $_POST['subject'];
            
            $content = $_POST['content'];
            
            $headers = 'From: '.$_POST['name'].' <'.$_POST['email'].">\r\n";
            
            if (mail($name, $emailTo, $subject, $content, $headers)) {
                
                echo "<script>alert('Message Sent Successfully');</script>";
                 echo "<script>window.location = 'index.php';</script>";
                
            } else {
                
                $error = '<div class="alert alert-danger" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';
                
                
            }
            
        }
        
        
        
    }

?>

如果我仅包含主题,电子邮件和内容,则效果很好。这是我的代码:

<?php

    $error = ""; $successMessage = "";

    if ($_POST) {
        
        if (!$_POST["email"]) {
            
            $error .= "An email address is required.<br>";
            
        }
        
        if (!$_POST["content"]) {
            
            $error .= "The content field is required.<br>";
            
        }
        
        if (!$_POST["subject"]) {
            
            $error .= "The subject is required.<br>";
            
        }
        
        if ($_POST['email'] && filter_var($_POST["email"], FILTER_VALIDATE_EMAIL) === false) {
            
            $error .= "The email address is invalid.<br>";
            
        }
        
        if ($error != "") {
            
            $error = '<div class="alert alert-danger" role="alert"><p>There were error(s) in your form:</p>' . $error . '</div>';
            
        } else {
            
            $emailTo = "me@mydomain.com";
            
            $subject = $_POST['subject'];
            
            $content = $_POST['content'];
            
            $headers = "From: ".$_POST['email'];
            
            if (mail($emailTo, $subject, $content, $headers)) {
                
                 echo "<script>alert('Message Sent Successfully');</script>";
                 echo "<script>window.location = 'index.php';</script>";
                
                
            } else {
                
                $error = '<div class="alert alert-danger" role="alert"><p><strong>Your message couldn\'t be sent - please try again later</div>';
                
                
            }
            
        }
        
        
        
    }

?>

希望有人能看到我出了问题。 谢谢!

0 个答案:

没有答案