• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Get certain data from $_POST from a dinamic form

#1
[eluser]Juan Velandia[/eluser]
Hello everyone, I would like to ask for your help. I have created a form created from records in a data base table named ITEMS

The view:
Code:
echo form_open('item/print');
foreach ($items as $item)
     {    
      echo form_checkbox($item->id_item, $item->name_item);
      }
echo form_submit('submit', 'Submit selected items');
echo form_close();

I check some of the check boxes that I want and send the form to the controller:

The controller
Code:
$post = array();
foreach ( $_POST as $key => $value )
  {
    $post[$key] = $this->input->post($key);
  }

  print_r($post);

The problem is that I need to put into the $post array ONLY the values $item->id_item of the selected items in the form, but I also get the SUBMIT value within the array

What can I do, I dont want to use a if(!$key==submit) within the loop. Any ideas or previous thread will be highly appreciated!

#2
[eluser]noslen1[/eluser]
Have you tried
Code:
echo form_submit('', 'Submit selected items');
?

#3
[eluser]Juan Velandia[/eluser]
Hello noslen1, That could do the trick, but it won`t work if I have have more fields on the form that I would like to use. I think it has to do with the name an id of each checkbox, but Im still dealing with it. Thanks a lot.

#4
[eluser]Juan Velandia[/eluser]
I think I did it:
view:

Code:
echo form_open('item/print');
foreach ($items as $item)
     {    
      echo form_checkbox(item_name[], $item->id_item );
      }
echo form_submit('submit', 'Submit selected items');
echo form_close();

controller:

Code:
$post = array();
$items = $_POST['item_name'];
foreach($items as $p)

  {
    $post[$p] =$p;
  }

print_r($post);
echo '<pre>';

#5
[eluser]CroNiX[/eluser]
so make your checkbox names an array,
Code:
&lt;input type="checkbox" name="checkbox_array[&lt;?php echo $item-&gt;id_item; ?&gt;]" value="&lt;?php echo $item-&gt;name_item; ?&gt;" /&gt;

and then just use
Code:
$checkboxes = $this->input->post('checkbox_array');

foreach($checkboxes as $id => $value)
{
  $post[$id] = $value;
}
Or something similar...

PS - You shouldn't directly use the $_POST array... use input::post().

#6
[eluser]Juan Velandia[/eluser]
Thanks Cronix, I'll do it as you say


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.