title: jQuery works once in php foreach loop
body: <p>Using Codeigniter 3.0.3</p>
  
  <p>In the view there's a foreach loop to get all posts from database and at the end of each loop there's a form with post id to post data to controller to insert it to comments_table in database</p>
  
  <p>But form works only in first post or first loop but another loops does not work</p>
  
  <p>Inside model i just need commenter id from session and post id from post-id attr and comment_text from input[type="text"] called with comment-text</p>
  
  <p>here's javascript</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function() {
      $('#addcommentajax').submit(function() {
          var commenter_id = "&lt;?php echo $this-&gt;session-&gt;userdata('id'); ?&gt;";
          var comment_text = $("#comment-text").val();
          var post_id = $(this).attr('post-id');
              jQuery.ajax({
                  type: "POST",
                  url: "&lt;?php echo base_url('home/addcomment'); ?&gt;",
                  dataType: "json",
                  data: {commenter_id, post_id, comment_text},
                  success: function(commented) {
                      if(commented == true) {
                          window.location.reload(true);
                          alert('commented');
                      }
                  }
              });
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>And here's the php foreach loop to display posts with his own comment form</p>
  
  <pre><code>              &lt;?php if($allposts): ?&gt;
                      &lt;?php foreach ($allposts as $post): ?&gt;
                          &lt;div class="panel panel-default"&gt;
                              &lt;div class="panel-body"&gt;
                                  &lt;div class="col-md-12" style="padding: 0;"&gt;
                                      &lt;img class="img-rounded pull-left" src="&lt;? echo base_url($post-&gt;image) ?&gt;" width=30px height=30px&gt;
                                      &lt;div style="margin-top: 5px;"&gt;
                                          &lt;a href="&lt;? echo base_url('home?p=Profile&amp;u='.$post-&gt;post_author) ?&gt;"&gt;&lt;small style="margin-left: 5px;"&gt;&lt;?=$post-&gt;post_author?&gt;&lt;/small&gt;&lt;/a&gt;
                                          &lt;span style="font-size: 75%;" class="text-muted pull-right"&gt;&lt;span class="fa fa-clock-o"&gt;&lt;/span&gt;
                                          &lt;?php $post_date = $post-&gt;post_date; $now = time(); $units = 1; ?&gt;
                                              &lt;? echo timespan($post_date, $now, $units) ?&gt;
                                                  ago&lt;/span&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;div class="clear"&gt;&lt;/div&gt;
                                  &lt;hr class="myown"&gt;
                                  &lt;div class="col-md-12 text-muted"&gt;&lt;small&gt;&lt;?=$post-&gt;post_text?&gt;&lt;/small&gt;&lt;/div&gt;
                                  &lt;div class="clear"&gt;&lt;/div&gt;
                                  &lt;hr class="myown"&gt;
                                  &lt;div class="btn-group"&gt;
                                      &lt;?php if($this-&gt;users_model-&gt;liked($post-&gt;pid)): ?&gt;
                                          &lt;a class="btn btn-default btn-sm disabled"&gt;&lt;span class="fa fa-check"&gt;&lt;/span&gt; Liked&lt;/a&gt;
                                          &lt;?php else: ?&gt;
                                              &lt;a class="btn btn-default btn-sm like-btn" post-id="&lt;?=$post-&gt;pid?&gt;"&gt;&lt;span data-toggle-tooltip="tooltip" data-placement="top" title="Like" class="fa fa-thumbs-up fa-lg"&gt;&lt;/span&gt;&lt;/a&gt;
                                              &lt;?php endif; ?&gt;
                                                  &lt;a class="btn btn-default btn-sm" data-toggle="collapse" data-target="#&lt;?=$post-&gt;pid?&gt;Comment"&gt;&lt;span data-toggle-tooltip="tooltip" data-placement="top" title="Comment" class="fa fa-comment fa-lg"&gt;&lt;/span&gt;&lt;/a&gt;
                                                  &lt;a class="btn btn-default btn-sm"&gt;&lt;span data-toggle-tooltip="tooltip" data-placement="top" title="Share" class="fa fa-share fa-lg"&gt;&lt;/span&gt;&lt;/a&gt;
                                  &lt;/div&gt;
                                  &lt;div class="pull-right-custom text-muted"&gt;&lt;small&gt;&lt;?=$post-&gt;post_likes?&gt; Like - &lt;?=$post-&gt;post_comments?&gt; Comment&lt;/small&gt;&lt;/div&gt;
                                  &lt;div class="clear"&gt;&lt;/div&gt;
                                  &lt;div id="&lt;?=$post-&gt;pid?&gt;Comment" class="collapse"&gt;
                                      &lt;br&gt;
                                      &lt;form name="addcommentajax" id="addcommentajax" post-id="&lt;?=$post-&gt;pid?&gt;" class="form-horizontal" role="form" method="POST"&gt;
                                          &lt;div class="input-group"&gt;
                                              &lt;input type="text" id="comment-text" class="form-control input-sm" placeholder="Add Comment"&gt;
                                              &lt;span class="input-group-btn"&gt;
                              &lt;button type="submit" class="btn btn-sm btn-default"&gt;Publish&lt;/button&gt;
                          &lt;/span&gt;
                                          &lt;/div&gt;
                                      &lt;/form&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;?php endforeach; ?&gt;
                              &lt;?php endif; ?&gt;
  </code></pre>