user2291730
user2291730

Reputation: 5

php mail() script not working

Hey guys I cannot for the life of me figure out where I went wrong here. When the form submits it takes it to a blank page. No errors or confirmed. Just Blank. Im assuming it is a syntax error but I just cant see it for some reason. Do you guys see anything? Here is my form code:

<form method="POST" action="mailtest.php">

<label>Name<span class="req">*</span></label>
<input name="name" placeholder="Type Here">

<label>Email<span class="req">*</span></label>
<input name="email" type="email" placeholder="Type Here">

<label>Subject</label>
<input name="subjectf" placeholder="Type Here">

<label>Message<span class="req">*</span></label>
<textarea name="message" placeholder="Type Here"></textarea>

<input class="submit" name="submit" type="submit" value="">
<span class="right" style="color:red">* represents a mandatory field.</span>
</form>

And here is the php script I have on the mailtest.php page:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $_POST['name'];
    $to = '[email protected]'; 
    $subject= 'new enquiry on website';
    $subjectf= $_POST['subjectf'];  
    $body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";

    if ($_POST['submit']) {
    if ($name != '' && $email != '' && $message != '') {         
            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 {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}
?>

Upvotes: 0

Views: 345

Answers (2)

Dinesh
Dinesh

Reputation: 3105

You should always first check whether the form was submitted so

remove if ($_POST['submit']) and put it on the top of the script..

also you should check whther the $_POST["submit"] was set.. the script should look like this:

if (isset($_POST['submit'])) {

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = $_POST['name'];
    $to = '[email protected]'; 
    $subject= 'new enquiry on website';
    $subjectf= $_POST['subjectf'];  
    $body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";


    if ($name != '' && $email != '' && $message != '') {         
            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 {
        echo '<p>You need to fill in all required fields!!</p>';
    }
}

Upvotes: 1

anonymous
anonymous

Reputation: 1

Set your submit input value to submit

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

Upvotes: 0

Related Questions