除非是空的,否则联系表格不会发送

时间:2014-07-25 20:03:26

标签: php html forms contact-form

我刚刚创建了一个联系表单,只有在任何输入字段中有任何文本的时候它才会被发送,它只显示联系页面上的索引模板,这不是我想要的。

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: Web design client'; 
$to = 'placeholder@mail.bcu.ac.uk'; 
$subject = 'Client';

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

if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }
}

?>

<form class="contact" method="post">
            <div class="slot-6-7">    
                <label>
                    Name
                </label>
                <input class="input" name="name" placeholder="Type Here">   
                <label>
                    Email
                </label>
                <input class="input" name="email" type="email" placeholder="Type Here">
            </div>
            <div class="slot-8-9">
                <label>
                    Message
                    </label>
                <textarea name="message" placeholder="Type Here"></textarea>    
                <input class="submit" id="submit" name="submit" type="submit" value="Submit">
            </div>
        </form>

电子邮件已更改隐私问题,该网站可在http://www.45-records.co.uk/contact/

上查看

我需要的是在有人输入查询时发送联系电子邮件,但我在代码中找不到错误。

提前致谢。

3 个答案:

答案 0 :(得分:2)

需要在所有标题指令后添加EOL字符("\n")。每条指令都需要以EOL字符结束。

From header应至少包含一个电子邮件地址以及可选名称。

建议使用isset()检查是否设置了$_POST['submit']

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$headers = 'From: Web design client <noreply@yourwebsite.com>' . "\n"; // Add EOL character

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

if ( isset( $_POST['submit'] ) ) { // Check if submit variable is set
    if (mail ('placeholder@mail.bcu.ac.uk', 'Client', $body, $headers)) { 
        echo '<p>Your message has been sent!</p>';
    } else { 
        echo '<p>Something went wrong, go back and try again!</p>'; 
    }
}

?>

答案 1 :(得分:1)

我会测试是否设置了提交而不是真实。

另外,我可能会遗漏一些东西,但不是你的表格缺少动作=“”

答案 2 :(得分:1)

这是检查表单的一种非常基本的方法。它应该告诉您(通过在变量中设置错误消息)哪些字段导致问题。

此外,from标头应使用电子邮件地址(见下文)。

希望这在某种程度上有所帮助。

<?php
$error = '0';
if(isset($_POST['name']) && $_POST['name'] != '') { $name = $_POST['name']; } else { $error = '1'; $error_message = 'Name Not Entered'; }
if(isset($_POST['email']) && $_POST['email'] != '') { $email = $_POST['email']; } else { $error = '1'; $error_message = 'Email Not Entered'; }
if(isset($_POST['message']) && $_POST['message'] != '') { $message = $_POST['message']; } else { $error = '1' $error_message = 'Message Not Entered'; }

if($error == '1') {
    echo $error_message;
} elseif($error == '0') {

    $from = 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>'; 
    $to = 'placeholder@mail.bcu.ac.uk'; 
    $subject = 'Client';

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

    $send_message = mail($to, $subject, $body, $from);

    if($send_message) {
        echo '<p>Your message has been sent!</p>';
    } else {
        echo '<p>Something Wrong</p>';
        }
}
?>

<form class="contact" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <div class="slot-6-7">    
        <label>
            Name
        </label>
        <input class="input" name="name" placeholder="Type Here">   
        <label>
            Email
        </label>
        <input class="input" name="email" type="email" placeholder="Type Here">
    </div>
    <div class="slot-8-9">
        <label>
            Message
            </label>
        <textarea name="message" placeholder="Type Here"></textarea>    
        <input class="submit" id="submit" name="submit" type="submit" value="Submit">
    </div>
</form>