title: Triggering AJAX function via <a href> - PHP
body: <p>I need to display a query via AJAX and to be able to do that, i have to match the ID of the product. Thus i thought of performing a POST method via AJAX to allow the execution of the query. </p>
  
  <p>Now i would like to do a trigger function for the AJAX such that the information will be shown on the page itself instead of being redirect to another page. How do i go about doing it ? I have added tried various place to add the Onclick function, however nothing seems to work.</p>
  
  <p>Any suggestion ?</p>
  
  <p>Thanks</p>
  
  <pre><code> echo '&lt;a onclick="showUser('.$row['ID'].')" method = "POST" action= "viewComments.php"&gt;Show Comments&lt;/a&gt;';
  
  &lt;script&gt;
  function showUser(str) {
    if (str=="") {
      document.getElementById("txtHint").innerHTML="";
      return;
    } 
    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("Post","viewComments.php?q="+str,true);
    xmlhttp.send();
  }
  &lt;/script&gt;
  </code></pre>