-2

我尝试创建的表单有问题。基本上,它不允许我将电子邮件发送给收件人,即使 PHP 代码是正确的。SO中很少有人试图提供帮助,但似乎代码不起作用。

<?php
$error = false;
$sent = false;

if(isset($_POST['submit'])) {
    if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['comments'])) {
        $error = true;
    } 
    else {

        $to = "linardsberzins@gmail.com";

        $name = trim($_POST['name']);
        $email = trim($_POST['email']);
        $comments = trim($_POST['comments']);

        $subject = "Contact Form";

        $message =  "Name: $name \r\n Email: $email \r\n Comments: $comments";
        $headers = "From:" . $name;
        $mailsent = mail($to, $subject, $message, $headers);

        if($mailsent) {
            $sent = true;
        }
   }
}
?>

HTML:

<?php if($error == true){ ?>
  <p class="error">Text</p>
<?php } if($sent == true) { ?>
  <p class="sent">Text</p>
<?php } ?>  
<div id="form">
<form name="contact" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <fieldset>
        <h4>Contact Me!</h4>
        <label for="name">Name:</label>
            <input type="text" name="name" id="name"/>
            <label for="email"/>Email:</label>
            <input type="text" name="email" id="email"/>
            <label for="comments" id="comments">Comments:</label>
            <textarea name="comments" id=""></textarea>
            <fieldset>
            <input class="btn" type="submit" name="submit"  class="submit" value="Send   email"/>
            <input class="btn" type="reset" value="Reset"/>            
            </fieldset> 
    </fieldset>
</form>

4

1 回答 1

1

不要依赖submit按钮,因为并非所有浏览器都将该按钮发送为POST,更改为其他输入:

if(isset($_POST['name'])) {
于 2012-08-29T10:28:04.023 回答