title: How to show message that user is already registered in html through php?
body: <p>I have an HTML form in which a user enter his/her email id to register everything is working great it checks the valid email id and also registered the email id ! But when I applied new code to check that the user is already registered or not it didn't work !! </p>
  
  <p>Below is my Html page</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&gt;
    &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
    &lt;title&gt;Sign Up - MOBTRICKS&lt;/title&gt;
    &lt;!-- CSS --&gt;
    &lt;link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Lobster"&gt;
    &lt;link rel='stylesheet' href='http://fonts.googleapis.com/css?family=Lato:400,700'&gt;
    &lt;link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css"&gt;
    &lt;link rel="stylesheet" href="assets/font-awesome/css/font-awesome.min.css"&gt;
    &lt;link rel="stylesheet" href="assets/css/style.css"&gt;
  
    &lt;!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --&gt;
    &lt;!-- WARNING: Respond.js doesn't work if you view the page via file:// --&gt;
    &lt;!--[if lt IE 9]&gt;
              &lt;script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"&gt;&lt;/script&gt;
              &lt;script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"&gt;&lt;/script&gt;
          &lt;![endif]--&gt;
  
    &lt;!-- Favicon and touch icons --&gt;
    &lt;link rel="shortcut icon" href="assets/ico/fa.png"&gt;
    &lt;link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png"&gt;
    &lt;link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png"&gt;
    &lt;link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png"&gt;
    &lt;link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png"&gt;
  
    &lt;!--  &lt;script type="text/javascript"&gt;
              function greeting(){
                  alert("Welcome ! Your Email : " + document.forms["form"]["email"].value + " has been registered under our records successfully !")
              }
          &lt;/script&gt;   --&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
    &lt;!-- Header --&gt;
    &lt;div class="container"&gt;
      &lt;div class="row header"&gt;
        &lt;div class="col-sm-4 logo"&gt;
          &lt;h1&gt;&lt;a href=#&gt;PQR&lt;/a&gt; &lt;span&gt;.&lt;/span&gt;&lt;/h1&gt;
        &lt;/div&gt;
        &lt;div class="col-sm-8 call-us"&gt;
          &lt;p&gt;Mob: &lt;span&gt;+91-9530803237&lt;/span&gt; | email: &lt;span&gt;ab.creations27@gmail.com&lt;/span&gt;
          &lt;/p&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;!-- Coming Soon --&gt;
    &lt;div class="coming-soon"&gt;
      &lt;div class="inner-bg"&gt;
        &lt;div class="container"&gt;
          &lt;div class="row"&gt;
            &lt;div class="col-sm-12"&gt;
  
              &lt;center&gt;
                &lt;i class="fa fa-cog fa-spin fa-3x fa-fw"&gt;&lt;/i&gt;
                &lt;span class="sr-only"&gt;Loading...&lt;/span&gt;
  
                &lt;h2&gt;We're Coming Soon&lt;/h2&gt;
                &lt;p&gt;We are working very hard on the new version of our site. It will bring a lot of new features. Stay tuned!&lt;/p&gt;
                &lt;div class="timer"&gt;
                  &lt;div class="days-wrapper"&gt;
                    &lt;span class="days"&gt;&lt;/span&gt; 
                    &lt;br&gt;days
                  &lt;/div&gt;
                  &lt;div class="hours-wrapper"&gt;
                    &lt;span class="hours"&gt;&lt;/span&gt; 
                    &lt;br&gt;hours
                  &lt;/div&gt;
                  &lt;div class="minutes-wrapper"&gt;
                    &lt;span class="minutes"&gt;&lt;/span&gt; 
                    &lt;br&gt;minutes
                  &lt;/div&gt;
                  &lt;div class="seconds-wrapper"&gt;
                    &lt;span class="seconds"&gt;&lt;/span&gt; 
                    &lt;br&gt;seconds
                  &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;!-- Content --&gt;
    &lt;div class="container"&gt;
      &lt;div class="row"&gt;
        &lt;div class="col-sm-12 subscribe"&gt;
          &lt;h3&gt;Subscribe to our newsletter !!&lt;/h3&gt;
          &lt;p&gt;Sign up now to our newsletter and you'll be one of the first to know when the site is ready:&lt;/p&gt;
  
          &lt;form class="form-inline" role="form" action="assets/subscribe.php" method="post"&gt;
            &lt;div class="form-group"&gt;
              &lt;label class="sr-only" for="subscribe-email"&gt;Email address&lt;/label&gt;
              &lt;input type="text" name="email" placeholder="Enter your email..." class="subscribe-email form-control" id="subscribe-email"&gt;
            &lt;/div&gt;
            &lt;button type="submit" class="btn"&gt;Subscribe&lt;/button&gt;
          &lt;/form&gt;
  
          ***
          &lt;div class="success-message"&gt;&lt;/div&gt;
          &lt;div class="error-message"&gt;&lt;/div&gt;***
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="row"&gt;
        &lt;div class="col-sm-12 social"&gt;
          &lt;a href="https://www.facebook.com/ashuart001/" data-toggle="tooltip" data-placement="top" title="Facebook"&gt;&lt;i class="fa fa-facebook"&gt;&lt;/i&gt;&lt;/a&gt;
          &lt;a href="#" data-toggle="tooltip" data-placement="top" title="Twitter"&gt;&lt;i class="fa fa-twitter"&gt;&lt;/i&gt;&lt;/a&gt;
  
          &lt;a href="https://github.com/ashu271994" data-toggle="tooltip" data-placement="top" title="GitHub"&gt;
            &lt;i class="fa fa-github"&gt;&lt;/i&gt;
          &lt;/a&gt;
  
          &lt;a href="https://plus.google.com/+AshishBharwal" data-toggle="tooltip" data-placement="top" title="Google Plus"&gt;&lt;i class="fa fa-google-plus"&gt;&lt;/i&gt;&lt;/a&gt;
          &lt;a href="https://in.pinterest.com/ashishbharwal/" data-toggle="tooltip" data-placement="top" title="Pinterest"&gt;&lt;i class="fa fa-pinterest"&gt;&lt;/i&gt;&lt;/a&gt;
  
          &lt;a href="mailto:ab.creations27@gmail.com?subject=MOBTRICKS-Signup &amp;body=Please send your query with valid details!" data-toggle="tooltip" data-placement="top" title="EMAIL"&gt;&lt;i class="fa fa-envelope-o"&gt;&lt;/i&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;!-- Footer --&gt;
    &lt;footer id="footer"&gt;
  
      &lt;ul class="copyright"&gt;
        &lt;li&gt;&amp;copy; &lt;a href="https://www.facebook.com/ASHISHBHARWAL001"&gt;ASHISH BHARWAL&lt;/a&gt;
        &lt;/li&gt;
        &lt;li&gt;Credits: &lt;a href="https://www.facebook.com/ashuart001/"&gt;AB-Creations&lt;/a&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    &lt;/footer&gt;
  
    &lt;!-- Javascript --&gt;
    &lt;script src="assets/js/jquery-1.11.1.min.js"&gt;&lt;/script&gt;
    &lt;script src="assets/bootstrap/js/bootstrap.min.js"&gt;&lt;/script&gt;
    ipt src="assets/js/jquery.backstretch.min.js"&gt;&lt;/script&gt;
    &lt;script src="assets/js/jquery.countdown.min.js"&gt;&lt;/script&gt;
    &lt;script src="assets/js/scripts.js"&gt;&lt;/script&gt;
  
    &lt;!--[if lt IE 10]&gt;
              &lt;script src="assets/js/placeholder.js"&gt;&lt;/script&gt;
          &lt;![endif]--&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Below is my Subscriber.php page </p>
  
  <pre><code>&lt;?php
  
  // Email address verification
  
  function isEmail($email)
  {
      return (preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]| [[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i", $email));
  }
  
  if ($_POST) {
  
      // Enter the email where you want to receive the notification when someone subscribes
      $emailTo = 'ab.creations27@gmail.com';
  
      $subscriber_email = addslashes(trim($_POST['email']));
  
      if (!isEmail($subscriber_email)) {
          $array            = array();
          $array['valid']   = 0;
          $array['message'] = 'Insert a valid email address!';
          echo json_encode($array);
          //   $msg="wrong answer";
          //   echo "&lt;script type='text/javascript'&gt;alert('$msg');&lt;/script&gt;";
      } else {
          $host = "somehostname";
          $user = "username";
          $pwd  = "password";
          $db   = "demo1";
  
          $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
          $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
          try {
              $sql_insert = "SELECT * FROM demotable WHERE subs='$subscriber_email'";
              $stmt1      = $conn-&gt;prepare($sql_insert);
  
              $stmt1-&gt;execute();
              $result = $stmt1-&gt;fetchColumn();
  
              if ($result == 0) {
                  $sql_insert = "INSERT INTO demotable (subs) 
                                         VALUES ('$subscriber_email')";
                  $stmt       = $conn-&gt;prepare($sql_insert);
  
                  $stmt-&gt;execute();
  
                  $array            = array();
                  $array['valid']   = 1;
                  $array['message'] = "Your Email : $subscriber_email has been registered with us ! Thanks for your subscription!";
                  echo json_encode($array);
              } else {
                  $array            = array();
                  $array['valid']   = 2;
                  $array['message'] = "You are already registered !!";
                  echo json_encode($array);
              }
  
          }
          catch (Exception $e) {
              die(var_dump($e));
          }
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>Now what is happening when I tried to add an invalid email id  then it shows Invalid Email id in  marked in HTML page but when I added a new user then  add the data in my table and show message but in case of the user who is already registered it didn't show any message !! I also tried to make new functions having <strong>"echo json_encode($array)"</strong>; But this also won't work !! Tell me what am I missing or what's my mistake !! I am trying to sort it from the last 3 days !!</p>
  
  <p>my scripts.js code below</p>
  
  <pre><code>    $('.subscribe form').submit(function(e) {
      e.preventDefault();
      var postdata = $('.subscribe form').serialize();
      $.ajax({
          type: 'POST',
          url: 'assets/subscribe.php',
          data: postdata,
          dataType: 'json',
          success: function(json) {
              if(json.valid == 0) {
                  $('.success-message').hide();
                  $('.error-message').hide();
                  $('.error-message').html(json.message);
                  $('.error-message').fadeIn();
              }
              else if (json.valid == 1){
                  $('.error-message').hide();
                  $('.success-message').hide();
                  $('.subscribe form').hide();
                  $('.success-message').html(json.message);
                  $('.success-message').fadeIn();
              }
               else {
                  $('.error-message').hide().empty();
                  $('.success-message').hide().empty();
                  $('.subscribe form').hide();
                  $('.success-message').html(json.message);
                  $('.success-message').fadeIn();
              }
          }
      });
  });
  </code></pre>