Solved

Mail script error checking problem

Posted on 2009-07-07
10
280 Views
Last Modified: 2012-05-07
Hi, I have a form mailer that I'm completely stumped on. It landed on my desk and I can't figure out what's wrong with it. It continually gives me empty field errors even when they're populated and won't send the email.

I'm sure someone here will have it sorted out in ten seconds but I'm more of a graphics guy and I'm just not seeing what's wrong. Any help would be appreciated!

Thanks in advance!
<?php
 
function createForm() { // grab variables from posted form, if any exist
	$name = $_POST['name'];
	$phone = $_POST['phone'];
	$email = $_POST['email'];
	$comments = $_POST['comments'];
 
 
	// build the form, populate the fields if needed
	echo '<form action="' . $_SERVER['PHP_SELF'] . '" method="POST">
	<p>FULL NAME:<br />
	<input name="name" type="text" id="name" size="40" value="' . $name . '"></p>
	<p>TELEPHONE:<br />
	<input name="phone" type="text" id="phone" size="40" value="' . $phone . '"></p>
	<p>EMAIL ADDRESS:<br />
	<input name="email" type="text" id="email" size="40" value="' . $email . '"></p>
	<p>QUESTION/COMMENT:<br />
	<textarea name="comments" id="comments" rows="5" style="width:100%;">' . $comments . '</textarea></p>
	<p><input type="submit" name="submit" value="Submit">&nbsp;<input type="reset" name="reset" value="Clear"></p>
	</form>';
} // end function createForm()
				
// if submit has been pressed 
if(isset($_POST['submit'])) {
	echo '<ul>';
				  
	// make sure all required fields are filled in and display errors if empty
	if(empty($name)) {
		$field1 = "0";
		echo '<li class="red strong">Name is required.</li>';
		} else {
		$field1 = "1"; }
		
	if(empty($phone)) {
		$field2 = "0";
		echo '<li class="red strong">Phone Number is required.</li>';
		} else {
		$field2 = "1"; }
				
	if(empty($email)) {
		$field3 = "0";	
		echo '<li class="red strong">Email Address is required.</li>';
		} else {
		$field3 = "1";	}
				
	if(empty($comments)) {
		$field4 = "0";
		echo '<li class="red strong">Comments are required.</li>';
		} else {
		$field4 = "1"; }
					
		echo '</ul>';
	
	// if any required fields are missing
	if(($field1=="0") || ($field2=="0") || ($field3=="0") || ($field4=="0")) {
		createForm();
		}
	
	// if all required fields are there
	if(($field1=="1") && ($field2=="1") && ($field3=="1") && ($field4=="1")) {
	
		// recipients email address
		$recipient = 'me@site.com';
	
		$name = $HTTP_POST_VARS['name'];
		$email = $HTTP_POST_VARS['email'];
		$phone = $HTTP_POST_VARS['phone'];
		$comments = $HTTP_POST_VARS['comments'];	
		
		// format the message for mail
		$comments = stripslashes($comments);
		$now = date('l F dS, Y g:i:s a');
		$message = "The following is a message from the website sent on $now.\n\nFrom: $name\nEmail: $email\nPhone Number: $phone\n\n\n$comments";
		
		// add email subject and headers
		$subject = "Email from website";
		$headers = "From: $name <$email>\n";
		$headers .= "Reply-To: $email\r\n";
	
		// sends the mail
		mail($recipient,$subject,$message,$headers);
		
		echo "<p class=\"strong bigger red\">Your email has been sent!</p>";
		echo "<p>Thank you for contacting us. We will do our best to reply to you as soon as is possible. For the quickest service, feel free to give us a phone call.</p>";
		echo "<p><a href=\"contact.php\">Send another message &#187;</a></p>";
		}
	} else {	
		createForm(); 
	} 			
?>

Open in new window

0
Comment
Question by:kingsburymedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 3

Expert Comment

by:Michal-Drozd
ID: 24797451
first do not use $HTTP_POST_VARS, it is deprecated, use $POST instead for grabbing values from post form
0
 
LVL 5

Author Comment

by:kingsburymedia
ID: 24797470
Ah yes, much appreciated. Still doesn't solve the problem but thanks for the heads up!
0
 
LVL 3

Expert Comment

by:Michal-Drozd
ID: 24797521
sorry i mean $_POST, dont $POST
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 3

Expert Comment

by:Michal-Drozd
ID: 24797523
try it
0
 
LVL 5

Author Comment

by:kingsburymedia
ID: 24797557
I figured you meant $_POST :) I did try it and it still doesn't work.
0
 
LVL 3

Expert Comment

by:Michal-Drozd
ID: 24797800
code is working fine, i tested it on my server

mail($recipient,$subject,$message,$headers) is called with right parameters (i injected some debug echos)

so problem is only with mail function
0
 
LVL 5

Author Comment

by:kingsburymedia
ID: 24797846
I know the posting is working fine because when it returns the 'no value' error for the field checking, it POSTS the previous values back into the fields, even though the error checking tells me they are empty LOL

It's still just not working for me. I wonder if it's difference between PHP versions that is causing the anomolies between us. I'm using PHP Version 5.2.5
0
 
LVL 3

Accepted Solution

by:
Michal-Drozd earned 500 total points
ID: 24798031
i am using php5.2.9-2 but it should have same behaviour

i see next problem in code

why u check variables like $name, $phone, $email...when they werent set ?

i mean line like following:
if(empty($name)) {

Open in new window

0
 
LVL 5

Author Comment

by:kingsburymedia
ID: 24798738
That did it, thanks a ton. I forgot that I was only checking for variables in the createForm function.

Cheers!!
0
 
LVL 5

Author Closing Comment

by:kingsburymedia
ID: 31600747
Thanks, glad you spotted that!!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question