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