title: Newsletter sign up page (PHP & MySQL)
body: <p>I am creating a newsletter sign up page with PHP and MySQL. I use this free source from <a href="https://github.com/pinceladasdaweb/Ajax-PHP-MySQL-Newsletter" rel="nofollow"><strong>this site</strong></a>.</p>
  
  <p>I have created a database in MySQL and has tested successfully and linked it to my PHP files. However, I am getting <strong>error Notice</strong> when I preview it (<code>subscribe.php</code>). Not sure what variables I have miss out in the code.</p>
  
  <blockquote>
    <p>Notice: Undefined index: signup-email in C:\xampp\htdocs\test\subscribe.php on line 15
    {"status":"error","message":"The email address field must not be blank"}**</p>
  </blockquote>
  
  <p>Here is my code (2 PHP files):</p>
  
  <p><strong>newsletters.php</strong></p>
  
  <pre><code>&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;newsletters&lt;/title&gt; 
  &lt;link rel="stylesheet" href="css/newsletter.css"&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;div id="newsletterform"&gt;
      &lt;div class="wrap"&gt;
          &lt;h3&gt;Get Email Update&lt;/h3&gt;
          &lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ullamcorper sapien luctus nisl laoreet, ac commodo tellus placerat. Etiam nec magna lacus. Curabitur quis felis vel nisl tincidunt hendrerit.&lt;/p&gt;
          &lt;form action="subscribe.php" method="post" id="newsletter" name="newsletter"&gt;
          &lt;input type="email" name="signup-email" id="signup-email" value="" placeholder="Insert email here" /&gt;
          &lt;input type="submit" value="Subscribe" name="signup-button" id="signup-button"&gt;
          &lt;span class="arrow"&gt;&lt;/span&gt;
          &lt;/form&gt;
          &lt;div id="response"&gt;&lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
  &lt;script type="text/javascript" src="js/jquery-1.11.0.min.js"&gt;&lt;/script&gt;
  &lt;script src="js/lib.js"&gt;&lt;/script&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>subscribe.php</strong></p>
  
  <pre><code>&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;newsletter-submit&lt;/title&gt; 
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;?php
  
  $host   = "localhost";
  $dbname = "cc_email_list";
  $user   = "ccemail";
  $pass   = "password";
  $email    = filter_var($_POST['signup-email'], FILTER_SANITIZE_EMAIL);
  $datetime = date('Y-m-d H:i:s');
  
  try {
      $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
  
      if (empty($email)) {
          $status = "error";
          $message = "The email address field must not be blank";
      } else if (!preg_match('/^[^0-9][A-z0-9._%+-]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/', $email)) {
          $status = "error";
          $message = "You must fill the field with a valid email address";
      } else {
          $existingSignup = $db-&gt;prepare("SELECT COUNT(*) FROM signups WHERE     signup_email_address='$email'");
          $existingSignup-&gt;execute();
          $data_exists = ($existingSignup-&gt;fetchColumn() &gt; 0) ? true : false;
  
          if (!$data_exists) {
              $sql = "INSERT INTO signups (signup_email_address, signup_date) VALUES (:email, :datetime)";
              $q = $db-&gt;prepare($sql);
              $q-&gt;execute(
                  array(
                      ':email' =&gt; $email,
                      ':datetime' =&gt; $datetime
              ));
  
              if ($q) {
                  $status = "success";
                  $message = "You have been successfully subscribed";
              } else {
                  $status = "error";
                  $message = "An error occurred, please try again";
              }
          } else {
              $status = "error";
              $message = "This email is already subscribed";
          }
      }
  
      $data = array(
          'status' =&gt; $status,
          'message' =&gt; $message
      );
  
      echo json_encode($data);
  
      $db = null;
  }
      catch(PDOException $e) {
      echo $e-&gt;getMessage();
  }
  
  ?&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>