anita
anita

Reputation: 1637

Contact form is not working?

I'm trying to create a simple contact page however when I submit the form, it leads me to the blank page contact-form.php. I used this article as reference: http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/ What am I doing wrong?

Code on index.php (corner divs are purely for styling purposes)

<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>

Code on contact-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>';
}

?>

Upvotes: 0

Views: 84

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

In your form, change:

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

to:

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

Plus, you may want to use this PHP mailing method, since the mail came into my SPAM when testing.

<?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>';
}

?>

And if you want to send as HTML use:

$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";

Upvotes: 1

user2976773
user2976773

Reputation: 59

change the button tag to an input tag

< input type ="submit" name="submit"/>

Upvotes: 1

Related Questions