PHP联系表单的服务器错误

时间:2015-07-27 19:05:37

标签: php html forms contact

不确定导致错误的原因。我已经尝试了好几次但是继续得到错误。欢迎任何帮助。谢谢。

这是我的表格:

<form class="form-horizontal" role="form" method="post" action="modalcontact.php">
    <div class="form-group">

        <div class="col-xs-8 col-xs-offset-2">
            <input type="text" class="form-control" id="name" name="name" placeholder="NAME" value="">
        </div>
    </div>
    <div class="form-group">

        <div class="col-xs-8 col-xs-offset-2">
            <input type="email" class="form-control" id="email" name="email" placeholder="EMAIL" value="">
        </div>
    </div>
    <div class="form-group">

        <div class="col-xs-8 col-xs-offset-2">
            <textarea class="form-control" rows="4" name="message" placeholder="MESSAGE"></textarea>
        </div>
    </div>

    <div class="form-group">
        <div class="col-xs-8 col-xs-offset-2">
            <input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-10 col-sm-offset-2">

        </div>
    </div>
</form>

这是托管服务器中modalcontact.php文件下的PHP:

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'L|D Contact Form'; 
        $to = 'myemail@email.com'; 
        $subject = 'Message from L|D Contact ';

        $body = "From: $name\n E-Mail: $email\n Message:\n $message";

        // Check if name has been entered
        if (!$_POST['name']) {
            $errName = 'Please enter your name';
        }

        // Check if email has been entered and is valid
        if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
            $errEmail = 'Please enter a valid email address';
        }

        //Check if message has been entered
        if (!$_POST['message']) {
            $errMessage = 'Please enter your message';
        }


// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
    if (mail ($to, $subject, $body, $from)) {
        echo '<div class="alert alert-success">Thank You! I will be in touch</div>';
    } else {
        echo '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
    }
}
    }
?>

1 个答案:

答案 0 :(得分:0)

尝试使用此代码替换from with header:

$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: abc@gmail.com';

我测试了你的代码,它工作正常。那么你的服务器上有什么问题......