Jump to content


Photo
- - - - -

PHP contact form sending blank emails

php email contact

  • Please log in to reply
4 replies to this topic

#1 vivalamac

vivalamac

    Forum Newcomer

  • Members
  • Pip
  • 4 posts

Posted 09 October 2017 - 11:05 PM

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>
<html>

<style>
* {
  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;
}
</style>
<body>

<form id="regForm" action="contact.php">
  <h1>Register:</h1>
  <!-- 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>
  <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>
 
  <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>
    </div>
  </div>
  <!-- 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>
  </div>
</form>

<script>
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:
  fixStepIndicator(n)
}

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:
    document.getElementById("regForm").submit();
    return false;
  }
  // Otherwise, display the correct tab:
  showTab(currentTab);
}

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";
}
</script>

</body>
</html>

<?php
$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\">";

        exit;

    
?>

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

 

Many Thanks,

Marc

Attached Files



#2 NOCK

NOCK

    Chief Troll Hunter

  • Privileged
  • PipPipPipPipPip
  • 1,600 posts
  • Gender:Male
  • Location:Eastbourne, East Sussex
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 10 October 2017 - 08:52 AM

Trim isn't a function, try changing it to 'trim'



#3 vivalamac

vivalamac

    Forum Newcomer

  • Members
  • Pip
  • 4 posts

Posted 10 October 2017 - 05:26 PM

Trim isn't a function, try changing it to 'trim'

Thanks, I did that but still get the exact same outcome. Just this again

Name:
City:
Email:
Tel:



#4 BrowserBugs

BrowserBugs

    Unhinged

  • Privileged
  • PipPipPipPipPip
  • 2,119 posts
  • Gender:Male
  • Location:Surrey, UK
  • Experience:Intermediate
  • Area of Expertise:I'm Learning

Posted 11 October 2017 - 08:27 AM

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.

<?php
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.
	exit();
} 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;
?> 


#5 rallport

rallport

    Laravel 5 Rocks

  • Moderators
  • PipPipPipPipPipPip
  • 5,950 posts
  • Gender:Male
  • Location:England, UK
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 22 October 2017 - 05:08 PM

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.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users