title: Where to use json_encode() and make this AJAX request
body: <p>I have to authenticate the user using a <code>getthedb.php</code> file which should receive data in JSON format (done using <code>json_encode()</code> function). This should send response again in JSON format. I want to use this structure that I have made and also a callback function which I don't know how to write and use. This username and password should be <code>json_decode()</code> in the <code>getthedb.php</code> file and username and password can be matched to the entries in the database. On success it should redirect the to the home page. 
             
             
             
             
             </p>
  
  <pre><code>    function checkPassword()
         {    
      var username = $("#username").val();
      var password =$("#password").val();
  
      $.ajax({
          url: "/getthedb.php",
          type:"POST",
          contentType: "application/json",
          data: JSON.stringify(userdetails),
          success: function mycallback() 
              {
  
                  alert("hi");    
              }
      });
  
          }
  
            &lt;/script&gt;
            &lt;body&gt;
  
        &lt;form method="post"&gt;
      &lt;label for="username"&gt;Username: &lt;/label&gt;
      &lt;input type="text" name="username" id="username" placeholder="Username" 
              required /&gt;
      &lt;label for ="password"&gt;Password&lt;/label&gt;
      &lt;input type="password" name="password" id="password" placeholder="Password"
               required /&gt;
      &lt;button value="Submit" onClick="checkPassword()"&gt;Submit&lt;/button&gt;
        &lt;/form&gt;
  
  
               &lt;/body&gt;
               &lt;/html&gt;
  </code></pre>
  
  <p>I have googled alot and found different methods every time. 
  xmlhttp object method not acceptable please. Provide me with some material to study for a good understanding of these ajax requests, where to write these, how to initiate these requests (like in xmlhttp we use <code>open()</code>, <code>setrequestheader()</code>, <code>send()</code> functions). I have just started with these things and really keen to learn to write web services. </p>