Welcome Guest, Not a member yet? Register   Sign In
how can i get the particular post id in foreach loop
#11

(06-25-2015, 02:39 AM)Chandini Wrote:
(06-24-2015, 07:37 AM)Wouter60 Wrote: In a very compact form, your view should have this structure:



PHP Code:
//html header etc.

<?php foreach($result as $row) : ?>
 
// info about this post

<div>
<?php
echo form_open('regcheck/comment_insert',array('name'=>'comments_form[]','id'=>'form_' $row->post_id));
echo 
form_hidden('post_id[]'$row->post_id);
echo 
form_input('post_comment[]',NULL,'size="40" class="ta-comment"');
echo 
form_close();
?> 
</div>
 
<?php endforeach; ?>

<script>

 $('.ta-comment').keypress(function(event) {
  var frm_id = $(this).parents('form').attr('id');
  if (event.which == 13 ) {
   event.preventDefault();
   $('#' + frm_id).submit();
  } 
  
 });  
 
</script>

//html footer etc. 
Take a good look at the Jquery script at the end. This script triggers the submit event only for the form where the user has typed text and hit Enter.

Now in your controller, put this code to fetch the posted values:


PHP Code:
$post_id $this->input->post('post_id[0]');
$post_comment $this->input->post('post_comment[0]');
//insert these values into your database 
The reason why you need to add [0] after the post_id and post_comment variables, is that these are arrays. Since only one form is posted, this array contains only one key (0).
Wow Thanks .. Its working ... really thanks to  Wouter60  Smile 



Is there any chance to submit the comment in ajax 

Manikanta
Reply
#12

(This post was last modified: 08-10-2015, 12:38 PM by Wouter60.)

Quote:Is there any chance to submit the comment in ajax 
Sure, in order to do that, first alter this line:
PHP Code:
echo form_input('post_comment[]',NULL,'size="40" class="ta-comment"'); 
 into:
PHP Code:
echo form_input('post_comment[]',NULL,'size="40" class="ta-comment" postid="' $row->post_id '"'); 

Then, in the script section, make the following changes:

Code:
<script>

$('.ta-comment').keypress(function(event) {
  if (event.which == 13 ) {
    event.preventDefault();
    url = "<?= base_url();?>regcheck/ajax_comment_insert";
    ctext = $(this).val();
    postid = $(this).attr('postid');
    $.ajax({

      url : url,
      type: "POST",
      data : { comment: ctext, post_id: postid }
    });
  } 
  
});  

</script>

And finally, you need to add a new method to your controller Regcheck, to make it insert the posted comment:

PHP Code:
public function ajax_comment_insert()
{
  $data = array(
   'post_id' => $this->input->post('post_id'),
   'comment' => $this->input->post('comment')
  );
  $this->db->insert('post_comments',$data);

Reply




Theme © iAndrew 2016 - Forum software by © MyBB