0

I am trying to post value in a hidden form but. I am unable to get the value in controller, I am new to codeigniter nd I am not sure whether I am doing the right thing.

My view is:

<tbody>
<?php foreach ($users as $user):
$id= $user['id']; ?>
<tr>

<td><?php echo $user['name'];?></td> 
<td><?php echo $user['sum_cmmnt']  ?>
<form class="add_cm" action="<?php echo base_url();?>comments/cmnt/" name="comm" method="post" >
    <a href="#" data-toggle="modal" data-target="#manage_cmnt">
        <input type="button" class="btn" name="user_id" value="<?php echo $id; ?>"/>

clicking on this button will open a pop-up window with all listed comment of user. When I am using fixed Id value it is working but using this it is unable to post the value in controller.

My controller is:

public function index()
    {
$this->load->model('modl_name');
$id = $this->input->post('user_id');
$this->data['users']=$this->modl_name->function($id);

I am not getting the value here in my controller. If I am using fixed value here its working fine.

Please help me...on this or suggest me other option if this is not right..

chiwangc
  • 3,566
  • 16
  • 26
  • 32
pawan
  • 31
  • 3
  • 7

2 Answers2

0

The input field type is incorrect, you have used a button instead of a hidden field. The correct syntax would be:

<input type="hidden" name="user_id" value="<?php echo $id; ?>"/>
Ben Broadley
  • 632
  • 6
  • 14
0

Your view code is confusing. You opened an Anchor <a> tag that is hooked with a modal and inside the anchor you added a button. I am sure, if you want the button to submit the post with the user_id value, you must change the input type as follow:

<a href="#" data-toggle="modal" data-target="#manage_cmnt">Show Comments</a>
 <input type="submit" class="btn" name="user_id" value="<?php echo $id; ?>"/>

and keep it outside of your anchor tag to allow the element to fire. Remember this will show the id as caption of button. To Avoid it, you can use

<button class="btn" type="submit" value="<?php echo $id;?>">Your Submit Text</button>

Try this and lets find out how it works.

Ariful Haque
  • 3,662
  • 5
  • 37
  • 59
  • still no luck, i am putting it inside the anchor because, i want clicking this will open a popup window and post the id same time. However using your suggestion it is still not posting id.. – pawan Apr 19 '15 at 17:14
  • @pawan Well you must use AJAX in this case. Your this code might not work as per your requirement. Submitting a form will refresh a page. At this time, a modal can not be popped up. What you can do is, write an AJAX function and execute it in modal show event `$('#manage_cmnt').on('shown', function() { //your ajax to post user_id })` – Ariful Haque Apr 19 '15 at 17:19
  • @pawan [This](http://stackoverflow.com/a/12190456/2627842) is a sample of modal show event. Might be helpful for you. – Ariful Haque Apr 19 '15 at 17:20