title: PHP: contact form error undefined index and variable
body: <p>Please help in solving this problem, i tried all the resolutions. I am a beginner in php and knows less about fixing bugs. I just copy pasted this code and now I am getting errors of undefined index and variables. like this one- "Notice: Undefined variable: errName in C:\xampp\htdocs\magpie\index1.php on line 27"
  and so on....
  If you have complete working php contact form code then post that too. Thanks in advance..</p>
  
  <pre><code>    &lt;?php
      if (isset($_POST["submit"])) {
      $name = $_POST['name'];
      $email = $_POST['email'];
      $message = $_POST['message'];
      $from = 'contact test'; 
      $to = 'example@domain.com'; 
      $subject = 'Message from Contact Demo ';
  
      $body ="From: $name\n E-Mail: $email\n Message:\n $message";
      // Check if name has been entered
      if (!isset($_POST['name'])) {
          $errName = 'Please enter your name';
      }
  
      // Check if email has been entered and is valid
      if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
          $errEmail = 'Please enter a valid email address';
      }
  
      //Check if message has been entered
      if (!isset($_POST['message'])) {
          $errMessage = 'Please enter your message';
      }
  
  // If there are no errors, send the email
  if (!$errName &amp;&amp; !$errEmail &amp;&amp; !$errMessage) {
  if (mail ($to, $subject, $body, $from)) {
      $result='&lt;div class="alert alert-success"&gt;Thank You! We will be in touch.&lt;/div&gt;';
  } else {
      $result='&lt;div class="alert alert-danger"&gt;Sorry there was an error sending your message. Please try again later.&lt;/div&gt;';
      }
  }
      }
  error_reporting (E_ALL ^ E_NOTICE);
  ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;title&gt;Untitled Document&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;div class="container"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col-md-6 col-md-offset-3"&gt;
              &lt;h1 class="page-header text-center"&gt;Contact Form Example&lt;/h1&gt;
              &lt;form class="form-horizontal" role="form" method="post" action="index1.php"&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="name" class="col-sm-2 control-label"&gt;Name&lt;/label&gt;
                      &lt;div class="col-sm-10"&gt;
                          &lt;input type="text" class="form-control" id="name" name="name" placeholder="First &amp; Last Name" value="&lt;?php echo htmlspecialchars($_POST['name']); ?&gt;"&gt;
                          &lt;?php echo "&lt;p class='text-danger'&gt;$errName&lt;/p&gt;";?&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="email" class="col-sm-2 control-label"&gt;Email&lt;/label&gt;
                      &lt;div class="col-sm-10"&gt;
                          &lt;input type="email" class="form-control" id="email" name="email" placeholder="example@domain.com" value="&lt;?php echo htmlspecialchars($_POST['email']); ?&gt;"&gt;
                          &lt;?php echo "&lt;p class='text-danger'&gt;$errEmail&lt;/p&gt;";?&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="message" class="col-sm-2 control-label"&gt;Message&lt;/label&gt;
                      &lt;div class="col-sm-10"&gt;
                          &lt;textarea class="form-control" rows="4" name="message"&gt;&lt;?php echo htmlspecialchars($_POST['message']);?&gt;&lt;/textarea&gt;
                          &lt;?php echo "&lt;p class='text-danger'&gt;$errMessage&lt;/p&gt;";?&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="col-sm-10 col-sm-offset-2"&gt;
                          &lt;input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary"&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;div class="col-sm-10 col-sm-offset-2"&gt;
                          &lt;?php echo $result; ?&gt;  
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/form&gt; 
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;/div&gt;   
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Please post any working contact form code or if you can find error in the above code, then please post in the answers. A Big thanks in advance... I am a photoshop, coreldraw and illustrator designer, know little coding of dot net but begginer in php.</p>