HTML:
<form class="contact_form" action="php/mail.php" method="post" name="contact_form">
<ul>
<li>
<label for="name">Name*</label>
<input type="text" placeholder="Your Name" required />
</li>
<li>
<label for="name">Email*</label>
<input type="email" name="email" placeholder="Your email address" required />
<span class="form_hint">Proper format "name@something.com"</span>
</li>
<li>
<label for="name">Website</label>
<input type="url" name="website" placeholder="Your website" required pattern="(http|https)://.+"/>
<span class="form_hint">Proper format "http://someaddress.com"</span>
</li>
<li>
<label for="message">Message:</label>
<textarea rows="6" cols="40" name="message" ></textarea>
</li>
<li>
<button class="submit" type="submit">Send</button>
</li>
</ul>
</form>
HTML
/* Subject and Email Variables */
$emailSubject = 'Contact Form';
$webMaster = 'email@address.com';
/* Gathering Data Variables */
$nameField = $_POST['Name'];
$emailField = $_POST['Email'];
$website = $_POST['Website'];
$messageField = $_POST['Message'];
$body = <<<EOD
<br><hr><br>
Name: $nameField <br>
Email: $emailField <br>
Message: $messageField <br>
EOD;
$headers = "From: $email
";
$headers .= "Content-type: text/html
";
$success = mail($webMaster, $emailSubject, $body, $headers);
/* Results Rendered as HTML */
I think you got typo there, change
$headers = "From: $email
";
to
$headers = "From: $emailField
";
Remember to validate the $emailField to avoid injections!
Update: Now that I can see the actual HTML for the form, there is some problems.
I think for
requires and ID field, so you should add one to every field in the form.
You also forgot to set name
in the name text field.
Lets add the name
attribute in the name field, and add the ID
field (which should be done for the other fields also):
<label for="name">Name*</label>
<input type="text" placeholder="Your Name" required />
to
<label for="name">Name*</label>
<input type="text" id="name" name="name" placeholder="Your Name" required />
$_POST['Name']
is case-sensetive, so you should change your code so the variables match the names in the html. example:
$nameField = $_POST['Name'];
should be
$nameField = $_POST['name'];
put $nameField = $_POST['Name']; $emailField = $_POST['Email']; $website = $_POST['Website']; $messageField = $_POST['Message'];
within a post function. After verifying $_post
is not empty. on form submit.
There are more problems, so:
name
attribute at the name text input$_POST['name'] !== $_POST['Name']
). This is the point why you received blank meesagesfor
attribute in labels has to correspond with input ID
Updated code:
<form class="contact_form" action="php/mail.php" method="post" name="contact_form">
<ul>
<li>
<label for="Name">Name*</label>
<input type="text" id="Name" name="Name" placeholder="Your Name" required />
</li>
<li>
<label for="Email">Email*</label>
<input type="email" name="Email" id="Email" placeholder="Your email address" required />
<span class="form_hint">Proper format "name@something.com"</span>
</li>
<li>
<label for="Website">Website</label>
<input type="url" id="Website" name="Website" placeholder="Your website" required pattern="(http|https)://.+"/>
<span class="form_hint">Proper format "http://someaddress.com"</span>
</li>
<li>
<label for="Message">Message:</label>
<textarea rows="6" id="Message" cols="40" name="Message" ></textarea>
</li>
<li>
<button class="submit" type="submit">Send</button>
</li>
</ul>
</form>