title: how to validate login form using json ajax and php
body: <pre><code>&lt;?php
      header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
      mysql_connect("localhost","root","root") or die("not connecting");
      mysql_select_db("demo")  ;
         echo"database connecting";
      if(isset($_POST['type']))
      {
          if($_POST['type']=="booking"){
              $dname = $_POST ['Name'];
              $dpwd = $_POST ['Pwd'];
              //$mail = $_POST ['Email'];    
  
              $query1 = "SELECT count(*) FROM user WHERE username='$dname' AND password='$dpwd'";
              $result1=mysql_query($query1);
              $num_row = mysqli_num_rows($result1);
              echo json_encode($result1);
               if( $num_row&gt;0 )
                 {
                while( $row=mysqli_fetch_array($result) ){
                     $_SESSION['userid'] = $row['userid'];
                  }
                 }
          }
      }
  
      else{
          echo "Invalid format";
      }
  ?&gt;   
  </code></pre>
  
  <p>Php code working fine placed in xampp server ,after putting in xampp server htdoc .</p>
  
  <p>this html code with validation with json and ajax. I am not getting any  confirmation message    </p>
  
  <pre><code> &lt;!DOCTYPE html&gt;
      &lt;html&gt;
        &lt;head&gt;
          &lt;title&gt;Admin Login&lt;/title&gt;
          &lt;!-- Bootstrap --&gt;
          &lt;link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen"&gt;
          &lt;link href="bootstrap/css/bootstrap-responsive.min.css" rel="stylesheet" media="screen"&gt;
          &lt;link href="assets/styles.css" rel="stylesheet" media="screen"&gt;
           &lt;!-- HTML5 shim, for IE6-8 support of HTML5 elements --&gt;
          &lt;!--[if lt IE 9]&gt;
            &lt;script src="http://html5shim.googlecode.com/svn/trunk/html5.js"&gt;&lt;/script&gt;
          &lt;![endif]--&gt;
          &lt;script src="js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"&gt;&lt;/script&gt;
           &lt;script src="vendors/jquery-1.9.1.min.js"&gt;&lt;/script&gt;
          &lt;script src="bootstrap/js/bootstrap.min.js"&gt;&lt;/script&gt;
       &lt;script src="js/jquery.validate.min.js"&gt;&lt;/script&gt;
  
      &lt;!--  using ajax and json to get data from login fields --&gt;
  
      &lt;script&gt;
      $(document).ready(function(){
  
          $("#loginform").validate({
              rules: {
                  username: "required",                           
                  password: {
                      required: true,
                      minlength: 2
                  },
                },
              messages: {
                     username: "Please enter your Name",
                  password: "Please enter your Mobile number"                         
  
              },
              submitHandler: function(form) {
                  alert("12");
                  // get values from textboxs 
                  var uname = $('#username').val();
                  var upasssword = $('#password').val();
  
                  $.ajax({
  
                      //url:"http://service4homes.com/Test/bookService4Homes.php",
                      url:"http://localhost/service4homes/bookService4Homes1.php",
                      type:"POST",
                      dataType:"json",
                      data:{type:"booking", Name:uname, Pwd:upassword },
                      //type: should be same in server code, otherwise code will not run
                      ContentType:"application/json",
                      success: function(){
                         // alert(JSON.stringify(response));
                         $("#result").html('Submitted successfully');
                          //alert("success");
                          window.location.href = 'index1.html';
                      },
                      error: function(err){
                        //  alert(JSON.stringify(err));
                              $("#result").html('There is error while submit');
                          //alert("fail");
                          window.location.href = 'index.html';
                      }
                  });
                  return false; // block regular submit
              }
          });
  
      });
      &lt;/script&gt;
  
  
        &lt;/head&gt;
        &lt;body id="login"&gt;
          &lt;div class="container" &gt;
          &lt;div id="result"&gt;&lt;/div&gt;
  
            &lt;form class="form-signin" id="loginform"  &gt;
              &lt;h2 class="form-signin-heading"&gt;Please sign in&lt;/h2&gt;
              &lt;input type="text" class="input-block-level" placeholder="Email address" id="username"&gt;
              &lt;input type="password" class="input-block-level" placeholder="Password" id="password"&gt;
             &lt;!--  &lt;label class="checkbox"&gt;
                &lt;input type="checkbox" value="remember-me"&gt; Remember me
              &lt;/label&gt; --&gt;
              &lt;button class="btn btn-large btn-primary" type="submit" &gt;Sign in&lt;/button&gt;
            &lt;/form&gt;
  
  
          &lt;/div&gt; &lt;!-- /container --&gt;
  
        &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>Php code working fine placed in xampp server   ,but in json validation is not working ,please help me  .</p>