-1

I keep getting an error as follows when i submit my form. I'm not sure what it is. I'm running the site on WAMP (windows) and im writing it in dreamweaver CS6. Please note I'm fairly new to PHP. Thanks!

Here is the code:

Notice: Undefined index: message in S:\wamp\www\latech\contact.php on line 29

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
  <?php
 
  // Check for Header Injections
  function has_header_injection($str) {
   return preg_match( "/[\r\n]/", $str );
  }
  
  
  if (isset($_POST['contact_submit'])) {
   
   // Assign trimmed form data to variables
   // Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
   $name = trim($_POST['name']);
   $email = trim($_POST['email']);
   $tel = trim($_POST['tel']);
   $msg = $_POST['message']; // no need to trim message
  
   // Check to see if $name or $email have header injections
   if (has_header_injection($name) || has_header_injection($email)) {
    
    die(); // If true, kill the script
    
   }
   
   if (!$name || !$email || !$msg) {
    echo '<h4 class="error">All fields required.</h4><a href="contact.php" class="button block">Go back and try again</a>';
    exit;
   }
   
   // Add the recipient email to a variable
   $to = "brad@brightsidestudios.ca";
   
   // Create a subject
   $subject = "$name sent a message via your contact form";
   
   // Construct the message
   $message .= "Name: $name\r\n";
   $message .= "Email: $email\r\n\r\n";
   $message .= "Message:\r\n$msg";
   
   // If the subscribe checkbox was checked
   if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
   
    // Add a new line to the $message
    $message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
    
   }
  
   $message = wordwrap($message, 72); // Keep the message neat n' tidy
  
   // Set the mail headers into a variable
   $headers = "MIME-Version: 1.0\r\n";
   $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
   $headers .= "From: " . $name . " <" . $email . ">\r\n";
   $headers .= "X-Priority: 1\r\n";
   $headers .= "X-MSMail-Priority: High\r\n\r\n";
  
   
   // Send the email!
   mail($to, $subject, $message, $headers);
  ?>
          <!-- Show success message after email has sent -->
  <h5>Thanks for contacting Anise Technologys!</h5>
  <p>Please allow 24 hours for a response.</p>
  <p><a href="/final" class="button block">&laquo; Go to Home Page</a></p>
   <?php
   } else {
  ?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you@website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
  <?php
   }
  ?>
</div>
</div>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
  <?php
 
  // Check for Header Injections
  function has_header_injection($str) {
   return preg_match( "/[\r\n]/", $str );
  }
  
  
  if (isset($_POST['contact_submit'])) {
   
   // Assign trimmed form data to variables
   // Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
   $name = trim($_POST['name']);
   $email = trim($_POST['email']);
   $tel = trim($_POST['tel']);
   $msg = $_POST['message']; // no need to trim message
  
   // Check to see if $name or $email have header injections
   if (has_header_injection($name) || has_header_injection($email)) {
    
    die(); // If true, kill the script
    
   }
   
   if (!$name || !$email || !$msg) {
    echo '<h4 class="error">All fields required.</h4><a href="contact.php" class="button block">Go back and try again</a>';
    exit;
   }
   
   // Add the recipient email to a variable
   $to = "brad@brightsidestudios.ca";
   
   // Create a subject
   $subject = "$name sent a message via your contact form";
   
   // Construct the message
   $message .= "Name: $name\r\n";
   $message .= "Email: $email\r\n\r\n";
   $message .= "Message:\r\n$msg";
   
   // If the subscribe checkbox was checked
   if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
   
    // Add a new line to the $message
    $message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
    
   }
  
   $message = wordwrap($message, 72); // Keep the message neat n' tidy
  
   // Set the mail headers into a variable
   $headers = "MIME-Version: 1.0\r\n";
   $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
   $headers .= "From: " . $name . " <" . $email . ">\r\n";
   $headers .= "X-Priority: 1\r\n";
   $headers .= "X-MSMail-Priority: High\r\n\r\n";
  
   
   // Send the email!
   mail($to, $subject, $message, $headers);
  ?>
          <!-- Show success message after email has sent -->
  <h5>Thanks for contacting Anise Technologys!</h5>
  <p>Please allow 24 hours for a response.</p>
  <p><a href="/final" class="button block">&laquo; Go to Home Page</a></p>
   <?php
   } else {
  ?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you@website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
  <?php
   }
  ?>
</div>
</div>
</body>
</html>
z6tanker
  • 3
  • 4
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Mar 29 '15 at 00:37

2 Answers2

1

Your name attribute is msg your calling it message in PHP.

<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>

$msg    = $_POST['message']; // no need to trim message

Also here...

$message .= "Name: $name\r\n";
            $message .= "Email: $email\r\n\r\n";
            $message .= "Message:\r\n$msg";

The first occurrence should be $message = because there is nothing to concatenate when it hasn't been initialized.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • I did that but im still getting this error: Notice: Undefined variable: message in S:\wamp\www\latech\contact.php on line 50 and Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in S:\wamp\www\latech\contact.php on line 73 – z6tanker Mar 28 '15 at 17:52
  • Updated with what the issue is and how to resolve it. – chris85 Mar 28 '15 at 18:18
  • What do you mean by "The first occurrence should be $message = because there is nothing to concatenate when it hasn't been initialized." I'm sorry I'm new to this :P – z6tanker Mar 28 '15 at 18:20
  • Here's a doc on it for future reference. http://php.net/manual/en/language.operators.string.php – chris85 Mar 28 '15 at 18:21
1

You are using

<textarea id="msg" name="msg" spellcheck="true" ></textarea>

Rather use

<textarea id="msg" name="message" spellcheck="true" ></textarea>
frunkad
  • 2,433
  • 1
  • 23
  • 35
  • I did that but im still getting this error: Notice: Undefined variable: message in S:\wamp\www\latech\contact.php on line 50 and Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in S:\wamp\www\latech\contact.php on line 73 – z6tanker Mar 28 '15 at 17:52