title: Check if email address already exists in the database using jQuery and PHP
body: <p>I am attempting to validate an email address if it already exists in a table, but this isn't working.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>&lt;html&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;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  $('#Submit').click(function() {
      var emailVal = $('#email').val(); // assuming this is a input text field
      $.post('checkemail.php', {'email' : emailVal}, function(data) {
          alert(data);
      });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form id="form1" name="form1" method="post" action="view.php"&gt;
    &lt;p&gt;
      &lt;input type="text" name="email" id="email" /&gt;
    &lt;/p&gt;
    &lt;p&gt;
       &lt;input type="submit" name="Submit" id="Submit" value="Submit" /&gt;
    &lt;/p&gt;
  &lt;/form&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>When the Submit button is clicked, it should validate first using the jQuery and call the checkemail.php file as shown below:</p>
  
  <pre><code>&lt;?php
  include("../includes/db.php");
  
  $sql = "SELECT email FROM lf_clients WHERE email = " .$_POST['email'];
  $select = mysqli_query($con, $sql);
  $row = mysqli_fetch_assoc($select);
  
  if (mysqli_num_rows &gt; 0) {
      echo "The email already exists.";
  }
  ?&gt;
  </code></pre>
  
  <p>However, when I click the submit button it goes to view.php instead of checkemail.php. Before it redirects to view.php, it should check first if the email already exists or not. If the email already exists, then it should not redirect to view.php.</p>