title: validating login form with php and javascript
body: <p>These gets the username and password inputted by the user and is expected to be sent to the validate.php file to check whether the records is present in the database or not. and receives a response.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function validatelogin() {
      var user=document.forms["login"]["username"].value;
      var pass=document.forms["login"]["password"].value;
      if(user==null || user=="") {
          alert("Please enter username");
          $('#username').focus();
          return false;
      } else if(pass==null || pass=="") {
          alert("Please enter password");
          $('#password').focus();
          return false;
      } else if(user!==null || user!=="" || pass!==null || pass!=="") {
          // These gets the username and password inputted by the user and is 
          // expected to be sent to the validate.php file to check whether the 
          // records is present in the database or not. and receives a response.
         $.ajax({
              url : "validate.php",
              type : 'POST',
              data :  $('#login').serialize(),
              success : function(msg) {
                  $('#login-box').html(msg);
              }
          });
          return false;
      } else {
          alert("form submitted");
      }
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>php file-used in querying the database to check existing records and is expected to trigger true or false response back to the ajax -please how can i do this.then from there the code can now decide whether to log in or not.</p>
  
  <pre><code>&lt;?php
      error_reporting(0);
      mysql_pconnect("localhost","root","");
      mysql_select_db("sim_tracker");
      $username=$_POST['username'];
      $password=$_POST['password'];
      mysql_query("select * from  tbl_user where 
          username='$username' and password='$password'");
      $row=mysql_affected_rows();
      if($row&gt;0) {
          return false;
          echo"$username,$password";
      }      
  ?&gt;
  </code></pre>
  
  <p>html</p>
  
  <pre><code>&lt;div id="login-box" class="login-popup"&gt;
      &lt;a href="#" class="close"&gt;
          &lt;img src="images/close_pop.png" class="btn_close" 
              title="Close Window" alt="Close" /&gt;
      &lt;/a&gt;
      &lt;form method="post"  id="login" class="signin" 
          action="#" onSubmit="return validatelogin()"&gt;
          &lt;fieldset class="textbox"&gt;     
              &lt;label class="username"&gt;
                  &lt;span&gt;Username or email&lt;/span&gt;
                  &lt;input id="username" name="username" value="" 
                      type="text" autocomplete="on" placeholder="Username"&gt;
              &lt;/label&gt;
              &lt;label class="password"&gt;
                  &lt;span&gt;Password&lt;/span&gt;
                  &lt;input id="password" name="password" value="" 
                      type="password" placeholder="Password"&gt;
              &lt;/label&gt;
              &lt;button class="submit button" type="submit"&gt;Sign in&lt;/button&gt;
              &lt;p&gt;
                  &lt;a class="forgot" href="#"&gt;Forgot your password?&lt;/a&gt;
              &lt;/p&gt;
          &lt;/fieldset&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>