Mail validating script will not display errors and won't sent the info to email

I am trying to learn PHP.

I have been working on this script for a while and am getting no where. Can someone please tell me what is going on? Why isn’t it displaying the errors and sending the information to a email address.


// I replaced my email address with non existent email address for security reasons
// Send the information to this email address
$to = "email@isp.net";

// Getting the variables from the contact form
$title = $HTTP_POST_VARS['title']; // Title
$fullname = $HTTP_POST_VARS['fullname']; // Full Name
$position = $HTTP_POST_VARS['position']; // Position Title
$company = $HTTP_POST_VARS['company']; // Company Name
$address = $HTTP_POST_VARS['address']; // Address
$phone = $HTTP_POST_VARS['phone']; // Phone Number
$fax = $HTTP_POST_VARS['fax']; // Fax Number
$mobile = $HTTP_POST_VARS['mobile']; // Mobile Number
$email = $HTTP_POST_VARS['email']; // Email Number
$subject = $HTTP_POST_VARS['subject']; // Subject of the email
$body = $HTTP_POST_VARS['body']; // Message/body

// The headers for the email
$header = "From: $email";

// The message of the email
$message =
	"Title: $title
	Full Name: $fullname
	Position Title: $position
	Company Name: $company
	Address: $address
	Phone Number: $phone
	Fax Number: $fax
	Mobile Number: $mobile
	Email Address: $email
	Message: $body";

// Checking that the required fields are filled in
if (isset ($_POST['send'])) {
	
	$problem = FALSE;
	
	if (empty ($_POST['title']))
	{
		$problem = TRUE;
  		print "<centre><b>Please enter your title. </b></centre>";
  		print "<centre><a href='javascript:history.back(1);'>Back</a></centre><br />";
	} else {
		$_POST = ""; // Clearing the field
	}

	if (empty ($_POST[$fullname])) 
	{
		$problem = TRUE;
  		print "<centre><b>Please enter your full name. </b></centre>";
  		print "<centre><a href='javascript:history.back(1);'>Back</a><br /></centre>";
	} else {
		$_POST = ""; // Clearing the field
	}
	
	if ($phone < 8) 
	{
		$problem = TRUE;
  		print "<centre><b>Please enter your phone number. </b></centre>";
  		print "<centre><a href='javascript:history.back(1);'>Back</a><br /></centre>";
	} else {
		$_POST = ""; // Clearing the field
	}
	
	if (empty ($_POST[$email])) 
	{
  		print "<centre><b>Please enter your email address. </b></centre>";
  		print "<centre><a href='javascript:history.back(1);'>Back</a><br /></centre>";
	} else {
		$_POST = ""; // Clearing the field
	}

	if (empty ($_POST[$subject])) 
	{
		$problem = TRUE;
 		print "<centre><b>Please enter a subject for this email. </b></centre>";
  		print "<centre><a href='javascript:history.back(1);'>Back</a><br /></centre>";
	} else {
		$_POST = ""; // Clearing the field
	}
	
	if (empty ($_POST[$body]))
	{
		$problem = TRUE;
  		print "<centre><b>Please enter a message for this email. </b></centre>";
  		print "<centre><a href='javascript:history.back(1);'>Back</a><br /></centre>";
	} else {
		$_POST = ""; // Clearing the field
	}

// No fields are empty now sending the information to the email address ($to)
	if ((!empty ($_POST['title'])) && (!empty ($_POST['fullname'])) && (!empty ($_POST['phone'])) && (!empty ($_POST['email']))
	&& (!empty ($_POST['subject'])) && (!empty ($_POST['body']))
	{
		print '<p><b>Message has been sent!</b></p>';
		mail($to, $subject, $message, $headers);
	}
}

For versions of php after 4.2 use $_POST rather than $HTTP_POST_VARS. Get rid of $_POST = “”; on all those lines, it isn’t doing what you think it is.

Thanks, Marble.

I have made those changes and now when I test it, it displays an error “Parse error: parse error in /var/home/basic/domainname/untitled.php on line 84” and I can’t see what could be wrong. Line 84 the 5th row from the bottom.

Try this…

if (!empty($_POST['title']) && !empty($_POST['fullname']) && !empty($_POST['phone']) && !empty($_POST['email']) && !empty($_POST['subject']) && !empty($_POST['body'])) {

Hope that works :smiley:

Thanks, KaiserSouze. That took away that error and now I have finally got the script to work.

No probs :thumb: The way that line was coded, you closed the if statement on the first condition. Make sure you keep all of the conditions you want to check for in an if statement in one set of brackets, and then doesn’t matter how many sets of brackets are contained within the initial set. Not explaining it very well so here’s an example:

if ((!empty ($_POST['title'])) && (!empty ($_POST['fullname'])) {

This only checks the first condition, and then isnt expecting the condition to continue.

if (!empty($_POST['title']) && !empty($_POST['fullname'])) {

Because the whole condition is contained in a pair of brackets, it happily cycles through, and you shouldn’t get any errors. :thumb:

Thanks for the tip, KaiserSouze. :slight_smile: