Jump to content
Sign in to follow this  

PHP contact form sending blank emails

Recommended Posts

Hi All,


I've managed to edit a form to fill in and submit to my email with php, but when ever i submit the form all I get is an email with the header name for each field but no information that was submitted. I've searched everywhere for some advice and tried aboout 30 different variations for a php script and I either get the same outcome or no email at all.

Can anyone give some advice on what I need to do to get the information submitted and not just the form name?

I've attached my html and php pages too.

<!DOCTYPE html>

* {
  box-sizing: border-box;
body {
  background-color: #f1f1f1;
#regForm {
  background-color: #ffffff;
  margin: 100px auto;
  font-family: Raleway;
  padding: 40px;
  width: 50%;
  min-width: 300px;
h1 {
  text-align: center;  
input {
  padding: 10px;
  width: 100%;
  font-size: 17px;
  font-family: Raleway;
  border: 1px solid #aaaaaa;
/* Mark input boxes that gets an error on validation: */
input.invalid {
  background-color: #ffdddd;
/* Hide all steps by default: */
.tab {
  display: none;
button {
  background-color: #4CAF50;
  color: #ffffff;
  border: none;
  padding: 10px 20px;
  font-size: 17px;
  font-family: Raleway;
  cursor: pointer;
button:hover {
  opacity: 0.8;
#prevBtn {
  background-color: #bbbbbb;
/* Make circles that indicate the steps of the form: */
.step {
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbbbbb;
  border: none;  
  border-radius: 50%;
  display: inline-block;
  opacity: 0.5;
.step.active {
  opacity: 1;
/* Mark the steps that are finished and valid: */
.step.finish {
  background-color: #4CAF50;

<form id="regForm" action="contact.php">
  <!-- One "tab" for each step in the form: -->
<div class="tab">
<form method="post" action="contact.php">
<label for="textfield">Name:</label>
 <label for="Name">Name:</label>
    <input type="text" name="Name" id="Name" required="required" class="required" minlength="2"/>

    <label for="City">City:</label>
    <input type="text" name="City" id="City" />
  <div class="tab">Contact Info:
    <label for="Email">Email:</label>
				<input type="text" name="Email" id="Email" />
    <label for="text">Tel:</label>
				<input type="text" name="Tel" id="Tel" />
  <div style="overflow:auto;">
    <div style="float:right;">
      <button type="button" id="prevBtn" onclick="nextPrev(-1)">Previous</button>
      <button type="button" id="nextBtn" onclick="nextPrev(1)">Next</button>
  <!-- Circles which indicates the steps of the form: -->
  <div style="text-align:center;margin-top:40px;">
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>
    <span class="step"></span>

var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the crurrent tab

function showTab(n) {
  // This function will display the specified tab of the form...
  var x = document.getElementsByClassName("tab");
  x[n].style.display = "block";
  //... and fix the Previous/Next buttons:
  if (n == 0) {
    document.getElementById("prevBtn").style.display = "none";
  } else {
    document.getElementById("prevBtn").style.display = "inline";
  if (n == (x.length - 1)) {
    document.getElementById("nextBtn").innerHTML = "Submit";
  } else {
    document.getElementById("nextBtn").innerHTML = "Next";
  //... and run a function that will display the correct step indicator:

function nextPrev(n) {
  // This function will figure out which tab to display
  var x = document.getElementsByClassName("tab");
  // Exit the function if any field in the current tab is invalid:
  if (n == 1 && !validateForm()) return false;
  // Hide the current tab:
  x[currentTab].style.display = "none";
  // Increase or decrease the current tab by 1:
  currentTab = currentTab + n;
  // if you have reached the end of the form...
  if (currentTab >= x.length) {
    // ... the form gets submitted:
    return false;
  // Otherwise, display the correct tab:

function validateForm() {
  // This function deals with validation of the form fields
  var x, y, i, valid = true;
  x = document.getElementsByClassName("tab");
  y = x[currentTab].getElementsByTagName("input");
  // A loop that checks every input field in the current tab:
  for (i = 0; i < y.length; i++) {
    // If a field is empty...
    if (y[i].value == "") {
      // add an "invalid" class to the field:
      y[i].className += " invalid";
      // and set the current valid status to false
      valid = false;
  // If the valid status is true, mark the step as finished and valid:
  if (valid) {
    document.getElementsByClassName("step")[currentTab].className += " finish";
  return valid; // return the valid status

function fixStepIndicator(n) {
  // This function removes the "active" class of all steps...
  var i, x = document.getElementsByClassName("step");
  for (i = 0; i < x.length; i++) {
    x[i].className = x[i].className.replace(" active", "");
  //... and adds the "active" class on the current step:
  x[n].className += " active";


$EmailTo = "myemail@gmail.com";
$EmailFrom = "myemail@gmail.com";
$Subject = "test email";
$EmailFrom = "macmotiondesign@gmail.com";
$EmailTo = "macmotiondesign@gmail.com";
$Subject = "Test";

$Name = $_POST['Name']; 
$City = Trim(stripslashes($_POST['City'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Tel = Trim(stripslashes($_POST['Tel'])); 

// validation

        //no errors, so send the email

        // prepare email body text
    $Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "City: ";
$Body .= $City;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\n";

        // send email 
        $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

        print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";



I've also attached an image of the email I receive.


Many Thanks,



Share this post

Link to post
Share on other sites

Hi, I think you need to work on debugging your scripts. Below is a rehash of your php assuming that this is all in the contact.php file, if the form is in the same page as the php then it's need adjusting.


Hope the comments make sense. The idea here is to find out if the form is actually posting values and they are not empty (possible, not sure what your js is doing). If they are not empty then it'll try and send the form. This is untested, just wanted to help you debug rather than posting an entire form and process. Also note there are other validations you should be looking at, this is a simple test to check the data is posting.

if(!$_SERVER['REQUEST_METHOD']=='POST') { // Check it was posted, if not then why are they here?
	header("Location: http://yourdomain.com/wheretheformis.php"); // Send them to the contact page.
} else {
	$execute = true; // I use this to action or not when validating.
	// Don't need these twice.
	//$EmailTo = "myemail@gmail.com";
	//$EmailFrom = "myemail@gmail.com";
	//$Subject = "test email";
	$EmailFrom = "macmotiondesign@gmail.com";
	$EmailTo = "macmotiondesign@gmail.com";
	$Subject = "Test Mail";
	$name = trim(stripslashes($_POST['Name']));
	$city = trim(stripslashes($_POST['City'])); 
	$email = trim(stripslashes($_POST['Email']));
	$tel = trim(stripslashes($_POST['Tel']));
	$msg = ""; // Using this to debug
	if($name=="") {
		$msg .= "Error! Name is empty.<br>"; $execute = false;
	if($city=="") {
		$msg .= "Error! City is empty.<br>"; $execute = false;
	if($email=="") {
		$msg .= "Error! Email is empty.<br>"; $execute = false;
	if($tel=="") {
		$msg .= "Error! Telephone is empty.<br>"; $execute = false;
	if($execute) {
		$headers = "From: Website <" . $EmailFrom . ">\r\nMIME-Version: 1.0\r\nContent-type: text/html;\r\nReply-To: " . $name . " <" . $email . ">\r\n";
		$email_message = "Name: " . $name . "<br>";
		$email_message .= "City: " . $city . "<br>";
		$email_message .= "Email: " . $email . "<br>";
		$email_message .= "Tel: " . $tel . "<br>";
		if(mail($EmailTo, $Subject, $email_message, $headers)) {
			$msg = "Sent";
			// Don't use this ...
			// print "<meta http-equiv=\"refresh\" content=\"0;URL=contactthanks.php\">";
			// Instead forward them to the success page. Commented out for now while testing.
			// header("Location: http://yourdomain.com/contactthanks.php");
			// exit();
		} else {
			$msg = "Not Sent";
echo $msg;

Share this post

Link to post
Share on other sites

It's to be expected.


If I visited yourdomainname.co.uk/contact.php the site would send a blank email.


At least check for a post request.


Before deploying that code into production I'd recommend having aread up on "PHP Contact Forms" at least, or better, use a framework.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Recently Browsing

    No registered users viewing this page.

  • Member Statistics

    • Total Members
    • Most Online

    Newest Member
  • Forum Statistics

    • Total Topics
    • Total Posts