2014-01-28 80 views
0

我想创建一个简单的联系页面,但是当我提交表单时,它会将我导向空白页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> 

代码上接触form.php的

<?php 

$name = $_POST['name']; 
$email = $_POST['email']; 
$message = $_POST['message']; 
$from = 'From: MessageAnita'; 
$to = '[email protected]'; 
$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>'; 
} 

?> 
+0

你有错误报告打开? –

+0

从输入中删除空值'value =“”',不需要它们。 – mdesdev

+0

你的其他和其他如果语句应该改变 – tarzanbappa

回答

1

变化的按钮标签的输入标签

< input type =“submit”name =“submit”/>

+0

这样做的伎俩,非常感谢你! – anita

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 = '[email protected]'; 
$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";