title: How to add onkeypress save in php form
body: <p>**This is my php <code>index.php</code> file. I want to type first name and last name type and auto it has to be save the database. I wrote the code using ajax. but, this is not working properly. Please can any one help me. **</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
  $connection = mysql_connect("localhost", "root", ""); 
  $db = mysql_select_db("type", $connection); 
  if(isset($_POST['submit'])){
  $firstName = $_POST['firstName'];
  $lastName = $_POST['lastName'];
  
  if($firstName !=''||$lastName !=''){
  //Insert Query of SQL
  $query = mysql_query("insert into users(firstName, lastName) values ('$firstName', '$lastName')");
  echo "&lt;br/&gt;&lt;br/&gt;&lt;span&gt;Data Inserted successfully...!!&lt;/span&gt;";
  }
  else{
  echo "&lt;p&gt;Insertion Failed &lt;br/&gt; Some Fields are Blank....!!&lt;/p&gt;";
  }
  }
  mysql_close($connection);
  ?&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta&gt;&lt;title&gt;Home Page&lt;/title&gt;
  &lt;script type="text/javascript"&gt;
  $(document).on('keyup','firstName','lastName',function(){
          var rel = $(this).attr('rel');
          var flatvalue = $(this).val();
          $("#firstName"+rel).val(flatvalue);
      });
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form action="" method="post"&gt; 
              &lt;label for="firstName"&gt;First Name&lt;/label&gt;
              &lt;input type="text" name="firstName" &gt;&lt;br&gt;&lt;br&gt;
              &lt;label for="lastName"&gt;Last Name&lt;/label&gt;
              &lt;input type="text" name="lastName" &gt;&lt;br&gt;&lt;br&gt;
              &lt;input type="submit" id="submit" name="submit" value="submit"/&gt;
          &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>