title: Generating and submitting multiple forms with AJAX
body: <p>I am building a forum site, but I am having trouble to figure out why my AJAX code not working correctly. The problem is:
  I am generating multiple forms (commenting section) via while-loop for each post that users make, and I have written the below AJAX script:  </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
  $(document).ready(function()
  {
      $(".comment_submit2").click(function(){
          var topic_id = &lt;?php echo $_GET['id']; ?&gt;;
          var post_id = // receiving the id from the form
          $.ajax({
              type: "POST",
              url: "comment_on_post.php",
              data: {topic_id: topic_id,post_id : post_id,comment : $('textarea#commenting2').val() }, //success on the first post only
              //data: $(this).serialize(), not working at all..
              cache: false,
              success: function(data){
                  alert(data);
                  location.replace("topic2.php?id=&lt;?php echo $_GET['id']; ?&gt;");
              }
          });
          return false;
      });
  });
  
  &lt;/script&gt;
  
  &lt;?php
  
  include 'dbh.php';
  //some other stuff...
  $result_posts = $conn -&gt; prepare("SELECT * FROM posts WHERE post_topic=:post_topic ORDER BY DATE(post_date) ASC");
  $result_posts -&gt; bindParam(':post_topic',$topic_id);
  $result_posts -&gt; execute();
  while ($row2 = $result_posts -&gt;fetch(PDO::FETCH_ASSOC)) 
  {
      ?&gt;
      //some other stuff...
      &lt;form name="form2" id=" &lt;?php echo $row2['post_id']; ?&gt; "&gt;
        &lt;textarea id="commenting2" placeholder="Leave a comment" cols="30"rows="5"&gt;&lt;/textarea&gt;
        &lt;br&gt;
        &lt;input type="submit" class="comment_submit2" value="Comment"&gt;
      &lt;/form&gt;
      //I need to pass the value id of form to ajax
      //some other stuff...
  &lt;?php  } ?&gt;
  </code></pre>
  
  <p>The "comment_on_post.php" file is below :</p>
  
  <pre><code>&lt;?php
  
  include 'dbh.php'; 
  session_start();
  
  if (isset($_SESSION['uid'])) {
      $user     = $_SESSION['uid'];
      $reply    = $_POST['comment'];
      $topic_id = $_POST['topic_id'];
      //$post_id  = ?
      $comments = $conn -&gt; prepare("INSERT INTO comments (uid,topic_id,post_id,comment,date) VALUES (:uid,:topic_id,:post_id,:reply,NOW())"); 
      $comments -&gt; bindParam(':uid',$user);
      $comments -&gt; bindParam(':topic_id',$topic_id);
      $comments -&gt; bindParam(':post_id',$topic_id);
      $comments -&gt; bindParam(':reply',$reply);
      $comments -&gt; execute(); 
  }
  
  ?&gt;
  </code></pre>
  
  <p>This code is working only with the first post. I  tried to <code>serialize()</code> but I think I am doing something wrong. Does anyone have an idea to solve this?</p>