2014-01-28 145 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";