简体   繁体   中英

Getting post value from form created in loop in CodeIgniter

I need post values from a form created in loop. Here is my view

<?php echo form_open('test/'.$id); ?>
   for($i=0;$i<count($array['value']);$i++) 
 <input type="text" name="name[]" value="<?php echo $array['value'][$i]?>" />
<input type="submit" /> 
  <?php }?>
<?php echo form_close(); ?>

My controller

function test ($id)
{ 
    echo $this->input->post('name'),
}

This returns an array of values, but I need the value from the id that is submitted.

I need to get variable from array that corresponds to the id eg if i=2 there will be 3 forms if I submit second form the value of second form alone needs to posted, but here I am getting as array

View Code could be like this

<?php 

foreach($array['value'] as $key=>$value)//for loop was out of php tag
{ 
  echo form_open('controller/test/'.$id);// see controller name here 
 ?>         
   <input type="text" name="name" value="<?php echo $value;?>" />
   <input type="submit" name="submit_<?php echo $key; ?>" /> 
<?php echo form_close(); 
} ?>

And Controller code should be

function test ($id)
{ 
 echo $id ."<br>";
 echo $this->input->post('name');    
}

create new form for each submit so it will post data of only one form wich you clicked and remove array from name so you can gate value as string instead of array

   for($i=0;$i<count($array['value']);$i++) 
      <?php echo form_open('test/'.$id); ?>
      <input type="text" name="name[]" value="<?php echo $array['value'][$i]?>" />
      <input type="submit" /> 
      <?php echo form_close(); ?>
  <?php }?>

then you can get as you expected

function test ($id)
{ 
    echo $this->input->post('name'),
}

Why don't you make three form this way??I haven't tested it but please check this way if that can give you the result:

 for($i=0;$i<count($array['value']);$i++) 
  <?php echo form_open('test/'.$id); ?>
    <input type="text" name="name" value="<?php echo $array['value'][$i]?>" />
        <input type="submit" /> 

        <?php echo form_close(); ?>
    <?php }?>


//this is controller function 
function test($id) 
 {
    echo $id;
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM