0

I am getting Undefined variable errors on the following lines of code. I personally have spent hours trying to fix it myself but for some reason I am unable to find the solution. I'm sure it's something simple I over looked. Any help or advice would be greatly appreciated. Thanks in advance for your time. I apologize for the poor format for some reason it didn't work out.

Undefined variable errors on the following lines of code:

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

if (!$error) {

==================================

$name = trim(stripslashes($_POST['CName']));
$email = trim(stripslashes($_POST['CEmail']));
$subject = trim(stripslashes($_POST['CSubject']));
$contact_message = trim(stripslashes($_POST['CMessage']));

// Check Name
if (strlen($name) < 2) {
    $error['name'] = "Please enter your 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 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: headers
$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['name'])) ? $error['name'] . "<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

}

?>

Shadowman
  • 9
  • 1
  • 5
  • Declare ` $error['name'] = array();` after this line `$contact_message = trim(stripslashes($_POST['CMessage'])); ` and after this instead of this checking `if (!$error) {` use `if(is_array($error) && empty($error)){` – Mahesh K S Jan 29 '18 at 03:43
  • thanks for the solution. I had solved the issue. it was a server side problem on my localhost. for some reason it did not like the php code. how ever when i ran it on my actual web site it ran fine w/ out any errors. not sure what the issue was as i didn't have time to investigate the matter. – Shadowman Feb 05 '18 at 01:39

0 Answers0