Home Ask Login Register

Developers Planet

Your answer is one click away!

user3102711 February 2016

PHP email issue with $Error message

On a Linux server, I have an ajax form that gets posted to a js script, which in turn posts to a php routine for sending a web site email contact form (Styleshout-Puremedia template). I know the post is occurring to js, and to the php page. I get an httpd server error on the php page that "$Error is undefined". However if I define it as NULL or '' it exists and therefore doesn't pass the test "if (!$Error)". If I create a hard-coded php script using the mail function, the e-mail works so php and sendmail are all configured properly. Can someone help as to why this php script doesn't work, or how to fix the "$Error" error?

PHP code:


// Replace this with your own email address
$siteOwnersEmail = 'info@mydomain.com';

if($_POST) {
   $fname = trim(stripslashes($_POST['contactFname']));
   $lname = trim(stripslashes($_POST['contactLname']));
   $email = trim(stripslashes($_POST['contactEmail']));
   $subject = trim(stripslashes($_POST['contactSubject']));
   $contact_message = trim(stripslashes($_POST['contactMessage']));

   // Check First Name
	if (strlen($fname) < 2) {
		$error['fname'] = "Please enter your first name.";
	// Check Last Name
	if (strlen($lname) < 2) {
		$error['lname'] = "Please enter your last name.";
	// Check Email
	if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
		$error['email'] = "Please enter a valid email address.";
	// Check Message
	if (strlen($contact_message) < 15) {
		$error['message'] = "Please enter your message. It should have at least 15 characters.";
   // Subject
	if ($subject == '') { $subject = "Contact Form Submission"; }

	// Set Name
	$name = $fname . " " . $lname;

   // Set Message
   $message = "Email from: " . $name . "<br />";


neilsimp1 February 2016

You can always just initialize $error as

$error = [];

and then check for errors by

if(count($errors) === 0){
    //no errors

Or, just use if(!isset($error)){//no errors}

Post Status

Asked in February 2016
Viewed 1,112 times
Voted 13
Answered 1 times


Leave an answer

Quote of the day: live life

Devs Planet ®

2014-2016 www.devsplanet.com

Devs Planet © all rights reserved