title: Live search with AJAX
body: <p>I need to build a live search but i don't know much about ajax and javascript, but i understand html, css and php.Can someone tell me what i'm doing wrong?</p>
  
  <pre><code>&lt;script&gt;
      function liveSearch(str) {
          if (str == "") {
              document.getElementById("txtHint").innerHTML = "";
              return;
          }
          else { 
              if (window.XMLHttpRequest) {
                  // code for IE7+, Firefox, Chrome, Opera, Safari
                  xmlhttp = new XMLHttpRequest();
              }
              else {
                  // code for IE6, IE5
                  xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
              }
  
              xmlhttp.onreadystatechange = function() {
                  if (xmlhttp.readyState == 4 &amp;&amp; xmlhttp.status == 200) {
                      document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                  }
              }
  
              xmlhttp.open("GET", "getuser.php?q=" + str, true);
              xmlhttp.send();
          }
      }
  &lt;/script&gt;
  
  &lt;input type="text" onkeypress="liveSearch(this.name)" name="searchWord"/&gt;
  
  &lt;?php
      $dbhost = 'localhost';
      $dbuser = 'root';
      $dbpass = '';
      $dbname = 'ezcart';
      $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
  
      if ($conn-&gt;connect_error) {
          die('Could not connect to the database: ' . $conn-&gt;connect_error);
      }
  
      $word = $_GET['q'];
      $sql= "SELECT prodNam FROM product WHERE prodNam LIKE '$q' ORDER BY prodNam ASC";
      $result = $conn-&gt;query($sql);
  
      if ($result-&gt;num_rows &gt; 0) {
          while($row = $result-&gt;fetch_assoc()) {
              echo "&lt;div&gt;&lt;p&gt;".$row[prodNam]."&lt;/p&gt;&lt;/div&gt;";
          }
      }
      $conn-&gt;close();
  ?&gt;
  </code></pre>