title: auto fill html form using php
body: <p>I have a HTML form which has 3 text input fields: <code>id</code>, <code>name</code>, and <code>contact</code>. My DB table has same named columns and the values are <code>id=1</code>, <code>name=mark</code> &amp; <code>contact=1234</code>. Now, if I enter '1' in my html form's id field &amp; press enter then how can the rest of the form's field (like: <code>name</code>, <code>contact</code>) will be automatically filled? </p>
  
  <p>What I've done so far is:</p>
  
  <p>my <strong>index.html</strong> file(I think its wrong because I didn't use JSON before):</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&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 type = javascript&gt;
  
  $("#email").bind("change", function(e){
    $.getJSON("lookup.php?email=" + $("#email").val(),
          function(data){
            $.each(data, function(i,item){
              if (item.field == "first_name") {
                $("#first_name").val(item.value);
              } else if (item.field == "last_name") {
                $("#last_name").val(item.value);
              }
            });
          });
  });
  
  
  &lt;/script&gt;&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;form&gt;
  &lt;input  type="text" name="email" id="email" /&gt;
  &lt;input type="text" name="first_name" id="first_name" /&gt;
  &lt;input type="text" name="last_name" id="last_name" /&gt;
  
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>my <strong>lookup.php</strong> file:</p>
  
  <pre><code>&lt;?php
  //look up the record based on email and get the firstname and lastname
  $email=$_GET['$email'];
  
  $con = mysql_connect("localhost", "root", "");
  if (!$con) {
      die('Could not connect: ' . mysql_error());
  }
  mysql_select_db("json", $con); //DB name= json
  
   mysql_select_db("json"); //DB name= json
  
    $result = mysql_query("SELECT * FROM json1 
       WHERE email LIKE '$email%'");      //DB table name=json1
  if (mysql_num_rows($result) == 1) {
          while ($myrow = mysql_fetch_array($result)) {
  //build the JSON array for return
  $json = array(array('field' =&gt; 'first_name', 
                      'value' =&gt; $firstName), 
                array('field' =&gt; 'last_name', 
                      'value' =&gt; $last_name));
  echo json_encode($json );
          }
  }
  ?&gt;
  </code></pre>