title: Commenting system on posts using ajax and php
body: <p>This is my code where I pass the <code>comment</code> and the post id (hidden input tag) to another php file <code>comments.php</code> which should insert it into db and show the result but its not happening.</p>
  
  <p>This is my form - part of code quoted in echo...I have an auto increment <code>pid</code> associated with each post so for every comment, the <code>comment</code> and the <code>pid</code> are stored in the database:</p>
  
  <pre><code>&lt;form  method=\"POST\" onSubmit=\"comment(); return false;\"&gt;
      &lt;input  id=\"comment\" type=\"text\" placeholder=\"Add Comment...\" name=\"comment\"&gt;
      &lt;input type=\"hidden\" name=\"pid\" value=\"".$row['pid']."\"&gt;
      &lt;div class=\"z\"&gt;&lt;input type=\"submit\" name=\"submit\" &gt;&lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>JavaScript - a function <code>comment()</code> is called for each post's comment form using <code>onsubmit</code>.</p>
  
  <pre><code>function comment() {
      jQuery.ajax({
          url: "comments.php",
          data: $('form').serialize(),
          type: "POST",
          success:function(data){
              $("#".$row['pid']).html(data);
          },
          error:function (){
          }
      });
  }
  </code></pre>
  
  <p>This is the <code>comments.php</code> code:</p>
  
  <pre><code>&lt;?php
      session_start();
      include 'db.php';
  
      $j =$_POST['comment'];
      $k = $_POST['pid'];
      $l =$_SESSION['uname'];
  
      $sql = "INSERT INTO comments (pid,name,comment) values     ('$k','$l','$j')";
      $r = $conn-&gt;prepare($sql);
      $r-&gt;execute();
      if($r) {
          echo '&lt;div class="comment"&gt;
                    &lt;a class="avatar"&gt;
                        &lt;img style="height:30px;"src="zmf.jpg"&gt;
                    &lt;/a&gt;
                    &lt;div class="content"&gt;
                        &lt;a class="author"&gt;'.$l.'&lt;/a&gt;
                        &lt;div class="metadata"&gt;
                            &lt;span class="date"&gt;Today at 5:42PM&lt;/span&gt;
                        &lt;/div&gt;
                        &lt;div class="text"&gt;';
                            echo   $j.'&lt;/div&gt;
                        &lt;div class="actions"&gt;
  
                        &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            ';
    }
  ?&gt;
  </code></pre>
  
  <p>What <code>comments.php</code> should return would be shown in the <code>div</code> in the <code>index.php</code> having an <code>id = #postid</code> which is already assigned in that index.php for each post:</p>
  
  <pre><code>&lt;div id=\"".$row['pid']."\"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><code>index.php</code> has such a <code>div</code> for each post so the post on which a comment is done, comment will be shown in that <code>div</code> placed below each post. Any help would be appreciated.</p>
  
  <p>i run two while loop to show all previuos comments for each post like this.
  first loop shows post and a nested while loop with each post that shows comment on each post..and a comment form for each post in while loop along with a div having an id = postid to show the ajax result..</p>
  
  <pre><code>    $q = $conn-&gt;prepare("SELECT * FROM posts ORDER BY pid DESC");
      $q-&gt;execute();
  
        while($row = $q-&gt;fetch(PDO::FETCH_ASSOC)) {
   #my post in div 
   #comment form
     &lt;form  method=\"POST\" onsubmit=\"comment()\"&gt;
      &lt;input  id=\"comment\" type=\"text\" placeholder=\"Add Comment...\" name=\"comment\"&gt;
      &lt;input type=\"hidden\" name=\"pid\" value=\"".$row['pid']."\"&gt;
      &lt;div class=\"z\"&gt;&lt;input type=\"submit\" name=\"submit\" &gt;&lt;/div&gt;
  &lt;/form&gt;
          #div where recent comment is shown using ajax
      &lt;div id=\"".$row['pid']."\"&gt;
        &lt;/div&gt;
  
   #nested while loop for comments
  
  $zmf = "SELECT * FROM comments WHERE pid = '" . $row['pid'] . "' ORDER BY                        comid DESC";
      $zed = $conn-&gt;prepare($zmf);
       $zed-&gt;execute();
        $run = $zed-&gt;fetch();
       while($run = $zed-&gt;fetch())
      {
       my div code and all..for comments
     }
  
       }
  </code></pre>
  
  <p>every post has an id pid ..
  but its nt working ..plz help</p>