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:
<?php
// 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 />";
   $message .= "Email address: " . $email . "<br />";
   $message .= "Message: <br />";
   $message .= $contact_message;
   $message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
   // Set From: header
   $from =  $name . " <" . $email . ">";
   // Email Headers
 $headers = "From: " . $from . "\r\n";
 $headers .= "Reply-To: ". $email . "\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
 $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
   if (!$error) {
      ini_set("sendmail_from", $siteOwnersEmail); // for windows server
      $mail = mail($siteOwnersEmail, $subject, $message, $headers);
  if ($mail) { echo "OK"; }
      else { echo "Something went wrong. Please try again."; }
  
 } # end if - no validation error
 else {
  $response = (isset($error['fname'])) ? $error['fname'] . "<br /> \n" : null;
  $response .= (isset($error['lname'])) ? $error['lname'] . "<br /> \n" : null;
  $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
  $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;
  
  echo $response;
 } # end if - there was a validation error
}
?>Thanks for your help!!
-Rob
 
    