0

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

Rob
  • 11
  • 4
  • You didn't initialize `$error` so if all cases are met it isnt set/defined. – chris85 Feb 08 '16 at 20:53
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – chris85 Feb 08 '16 at 20:55

1 Answers1

0

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}

neilsimp1
  • 1,242
  • 1
  • 11
  • 25
  • I tried this. It does stop the $Error message, thanks neilsimp1! Unfortunately, the mail still isn't working. I see no errors, but also no results. What can I do to troubleshoot further? – Rob Feb 08 '16 at 21:37
  • [Maybe something like this](stackoverflow.com/questions/3186725/how-can-i-get-the-error-message-for-the-mail-function)? Beyond that, I wouldn't know, it would probably have something to do with your environment. – neilsimp1 Feb 08 '16 at 22:22