title: insert data into database using ajax, javascript and php
body: <p>The problem is: My motive is to get the subject name from the admin and insert it into database. Then i want to give the result of my query onto the same web page without refreshing it as the ajax should do. The following code is giving wrong o/p. I tried many syntaxes but still wrong response from the code. Please tell me the correct way.</p>
  
  <p>My HTML code is:</p>
  
  <pre><code>&lt;form&gt;
      &lt;input type="text" name="subject" /&gt;
      &lt;div id="subject"&gt;&lt;/div&gt;
      &lt;input type="button" value="Submit" onclick="addSubject()" /&gt;
      &lt;script src="home.js"&gt;
      &lt;/script&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>My home.js code is:</p>
  
  <pre><code>function addSubject() {
      a=document.getElementsByName("subject")[0].value;
      destination="ajaxstoresubject.php?a:"+a+";";
      var xhr=new XMLHttpRequest();
      xhr.open("GET",destination,true);
      xhr.send();
      xhr.onreadystatechange=function() {
          if(xhr.readyState==4 &amp;&amp; xhr.status==200) {
              document.getElementById("Subject").innerHTML=xhr.responseText;
          }
      }
  }
  </code></pre>
  
  <p>ajaxstoresubject.php's code is:</p>
  
  <pre><code>&lt;?php
  $subject=$_GET["a"];
  $con=mysqli_connect("localhost","root","","doortolearn");
  if(!$con){
      echo("No database connection.");
  }
  $query="insert into subjects(sid) values('$subject')";
  $result=mysqli_query($con,$query);
  if($result){
      echo("Subject successfully inserted");
  }
  else{
      echo("Subject could not be inserted. Might be, the subject already exists.");
  }
  ?&gt;
  </code></pre>
  
  <p>The problem is: It inserts an empty value into the sid column of subject table. I checked, sid is varchar still the problem. It is also not giving any output on the web Page.</p>
  
  <p>Please don't suggest JQUERY. I don't know anything about that.</p>