title: PHP + AJAX + jQuery comment/reply system stops working after first reply is pulled
body: <p>Basically, I have a comment system that I'm trying to add AJAX/jQuery support to so that users can view "in reply to" comments, similar to how Youtube handles comments and replies.</p>
  
  <p>Every comment associated with the article is displayed (comment_view.php), and if it is a reply to another comment, a button is displayed that a user can click to view the comment that it is in reply to.</p>
  
  <p>The initial comment shows up fine, and then when the "in reply to" button is clicked, the reply shows up as expected too! However, when the next "in reply to" button is clicked from the reply that was just returned, nothing happens.</p>
  
  <p>Another strange thing is that if I get rid of the <code>style="display: none;"</code> on the reply div, it seems to work, and pull all of the "reply to's" all the way through, albeit without the jQuery "show" animation.</p>
  
  <p>Any thoughts of what's going on here?</p>
  
  <p>In all, there are 3 files involved: article.php, comment_view.php, and getreply.php, detailed below:</p>
  
  <p><strong>article.php (snippet)</strong></p>
  
  <p>This file hits the database and pulls an article and it's associated comments</p>
  
  <pre><code>foreach ($comments as $comment) {
      echo '&lt;div id="comment'.$comment['id'].'" class="comment"&gt;';
      require 'comment_view.php';
      echo '&lt;/div&gt;';
  }
  </code></pre>
  
  <p><strong>comment_view.php</strong></p>
  
  <p>This file is included by both article.php (above) and getreply.php (below, which is fetched with AJAX from the JavaScript below)</p>
  
  <pre><code>$_SESSION['rnum']++; //session variable that gets incremented to make sure each reply div id is unique
  
  if ($comment['reply_to_id'] != null) {
      echo '&lt;div id="reply'.$_SESSION['rnum'].'" style="display: none;"&gt;&lt;/div&gt;'; //empty/hidden div to hold the future contents of a reply, which ends up being this file, instantiated by getreply.php (below) with a new $comment from database
  }
  
  echo '&lt;div class="content"&gt;';
  echo nl2br($comment['content']);
  if ($comment['reply_to_id'] != null) {
      echo '&lt;br/&gt;&lt;button id="showreply'.$_SESSION['rnum'].'" onclick="showReply('.$comment['reply_to_id'].','.$_SESSION['rnum'].'); this.disabled = true;"&gt;In reply to -&gt; '.$comment['reply_to_id'].'&lt;/button&gt;';
      echo '&lt;script&gt;$("#showreply'.$_SESSION['rnum'].'").click(function () { $("#reply'.$_SESSION['rnum'].'").show("slow"); });&lt;/script&gt;';
  }
  echo '&lt;/div&gt;'; //content
  </code></pre>
  
  <p><strong>JavaScript (located in header of article.php)</strong></p>
  
  <p>This AJAX function takes the comid (comment ID) and passes it to getreply.php, so that it knows which reply to pull from the database. It also takes rnum to figure out the unique ID of the reply div to replace the contents of.</p>
  
  <pre><code>&lt;script&gt;
  function showReply(comid,rnum) {
      if (comid=="") {
          document.getElementById("reply"+rnum).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("reply"+rnum).innerHTML=xmlhttp.responseText;
          }
      }
      xmlhttp.open("GET","getreply.php?id="+comid,true);
      xmlhttp.send();
  }
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>getreply.php</strong></p>
  
  <p>This file takes the comid (comment ID) passed by AJAX to pull the reply comment from the database, and then reinvoke comment_view.php (which should then generate another new unique rnum for the next potential reply, and spit out the comment)</p>
  
  <pre><code>session_start();
  
  $query = "SELECT * FROM comments WHERE id = ".$_GET['id'];
  $result = $mysqli-&gt;query($query) or die($mysqli-&gt;error. " [" . __LINE__ . "]");
  
  if ($result-&gt;num_rows == 1) {
      $comment = $result-&gt;fetch_assoc();
      require 'comment_view.php';
  }
  </code></pre>
  
  <p>That was a bit lengthier that I anticipated, but thanks in advance for any help or ideas...</p>