I appologise if this is a redundant question, but I have been trying to figure out this problem for hours! I am trying to get a contact form to send an email from my website. The "message failed" error keeps happening even though I do receive an email, except all fields are blank. There is some sort of problem transferring the data from the form to the email template. I have checked and double checked all variables used but can not find the issue. Below is the code.

<?php
$nameField = $_POST['name']; 
$emailField = $_POST['email']; 
$phoneField = $_POST['phone'];
$messageField = $_POST['message']; 

$receiver = "xxx@example.com" ;
$subject = 'Message from web site visitor '.$nameField;
$headers = 'From: '.$emailField; 

$body = 'Name:'.$nameField."\n";
$body .='Email:'.$emailField."\n";
$body .='Phone:'.$phoneField."\n";
$body .='Message:'.$messageField;

$send = mail($receiver, $subject, $body, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'contacts.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please try again.');
        window.location = 'contacts.html';
    </script>
<?php
}
?>

and from the html file:

<form id="contact-form" method="post" action="contact.php">
    <fieldset>
        <label><input type="text" name="name" value="Name" 
            onFocus="if(this.value=='Name'){this.value=''}" 
            onBlur="if(this.value==''){this.value='Name'}"> </label>
        <label><input type="text" name="email" value="Email" 
            onFocus="if(this.value=='Email'){this.value=''}" 
            onBlur="if(this.value==''){this.value='Email'}">    </label>
        <label><input type="text" name="phone" value="Phone" 
            onFocus="if(this.value=='Phone'){this.value=''}" 
            onBlur="if(this.value==''){this.value='Phone'}">    </label>
        <textarea name="message" onFocus="if(this.value=='Message'){this.value=''}" onBlur="if(this.value==''){this.value='Message'}">Message</textarea>

        <a href="#" class="button1" onClick="document.getElementById('contact-form').reset()">clear</a>
        <a href="#" class="button1" onClick="document.getElementById('contact-form').submit()">send</a>
    </fieldset>
</form>

Thank you to anyone that can help!

Joe

Ok thanks! That was one error, and it says it was successfully sent. I am still receiving an email with all fields blank, like below.

Name:
Email:
Phone:
Message:

Could you temorarily insert print_r($_POST); onto line 1 so we can see all of the contents of $_POST?

Well, I was using a different computer earlier and it wasn't working. I just tried it on my home pc and it all seems to be working correctly? Maybe a problem with the browser earlier? Anyway Thank you for your help.

P.S. I have one more question. Is there a way to ensure that these messages are not sent to my spam folder?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.