-
Wouter60 Posting Freak
    
-
Posts: 851
Threads: 38
Joined: Feb 2015
Reputation:
77
You should name your fields as arrays, by adding square brackets after the field's name, like this:
PHP Code: <textarea name="comment_text[]" rows=3 cols=40></textarea> <input type="hidden" name="post_id[]" value="<?= post_id;?>" />
When your form is posted, you can loop through the arrays like this:
PHP Code: $posts = $this->input->post('post_id'); //returns an array!! $comments = $this->input->post('comment_text'); //also returns an array foreach ($posts as $key=>$post_id) { if (empty($comments[$key]) continue; //don't save empty comments fields $data = array( 'post_id' => $post_id, 'comment' => $comments[$key] ); $this->db->insert('table',$data); }
-
Chandini BAD BOY
  
-
Posts: 61
Threads: 34
Joined: Dec 2014
Reputation:
0
(06-16-2015, 12:48 PM)Wouter60 Wrote: You should name your fields as arrays, by adding square brackets after the field's name, like this:
PHP Code: <textarea name="comment_text[]" rows=3 cols=40></textarea> <input type="hidden" name="post_id[]" value="<?= post_id;?>" />
When your form is posted, you can loop through the arrays like this:
PHP Code: $posts = $this->input->post('post_id'); //returns an array!! $comments = $this->input->post('comment_text'); //also returns an array foreach ($posts as $key=>$post_id) { if (empty($comments[$key]) continue; //don't save empty comments fields $data = array( 'post_id' => $post_id, 'comment' => $comments[$key] ); $this->db->insert('table',$data); }
Manikanta
-
Wouter60 Posting Freak
    
-
Posts: 851
Threads: 38
Joined: Feb 2015
Reputation:
77
I think I found the problem.
Your view builds up a series of forms (within a foreach { } loop).
All forms have the same name: post_comment.
In your javascript function do_something(), you submit the form with the name post_comment. Since all forms have that name, only the first one is posted.
Now my question is: when the user hits the Enter key, do you want all comment-forms to be posted, or just the one where the user has filled-in some text and hit Enter?
-
Wouter60 Posting Freak
    
-
Posts: 851
Threads: 38
Joined: Feb 2015
Reputation:
77
06-24-2015, 07:37 AM
(This post was last modified: 06-24-2015, 07:38 AM by Wouter60.)
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).
-
Chandini BAD BOY
  
-
Posts: 61
Threads: 34
Joined: Dec 2014
Reputation:
0
(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
Manikanta
|