联系表格不起作用?

时间:2014-01-28 02:49:05

标签: php html forms

我正在尝试创建一个简单的联系页面但是当我提交表单时,它会将我引导到空白页面contact-form.php。我用这篇文章作为参考:http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/ 我做错了什么?

index.php上的代码(角落div仅用于造型)

<form action="contact-form.php" method="post" autocomplete="off" id="contact">
    <div class="corner"></div>
    <input type="text" required name="name" placeholder="Name" value="">
    <div class="corner"></div>
    <input type="text" required name="email" placeholder="Email" value="">
    <div class="corner"></div>
    <input type="text" required name="check" placeholder="Question of the day: What's 2 + 2 ?" value="">
    <div class="corner"></div>
    <textarea name="message" rows="25" cols="50" placeholder="Drop me a line!"></textarea>
    <button class="send" type="submit" name="submit">Send</button>
</form>

contact-form.php上的代码

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MessageAnita'; 
$to = 'myemail@gmail.com'; 
$subject = 'Hello';
$check = $_POST['check'];

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

if ($_POST['submit'] && $check == '4') {                 
    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>'; 
} 
} else if ($_POST['submit'] && $check != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
}

?>

2 个答案:

答案 0 :(得分:1)

将按钮标记更改为输入标记

&LT; input type =“submit”name =“submit”/&gt;

答案 1 :(得分:1)

在您的表单中,更改:

<button class="send" type="submit" name="submit">Send</button>

为:

<input type="submit" name="submit" value="Submit">

另外,您可能希望使用此PHP邮件方法,因为邮件在测试时会进入我的垃圾邮件。

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myemail@gmail.com'; 
$subject = 'Hello';
$check = $_POST['check'];

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

$headers = "From: $from" . "\r\n" .
            "Reply-To: $from" . "\r\n";

if ($_POST['submit'] && $check == '4') {

    if (mail ($to, $subject, $body, $headers)) { 

    echo '<p>Your message has been sent!</p>';
} else { 
    echo '<p>Something went wrong, go back and try again!</p>'; 
} 
} else if ($_POST['submit'] && $check != '4') {
    echo '<p>You answered the anti-spam question incorrectly!</p>';
}

?>

如果您想以HTML格式发送:

$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from" . "\r\n" .
            "Reply-To: $from" . "\r\n";