metaphor
metaphor

Reputation: 470

Codeigniter get form field value from array fields not working

I want get post fields value in my View, but all form did not get the value as well, i get an "Array" string in my form View when i click submit button.

enter image description here

I declared the fields in array => Controller :

$data6 = array(
     'no_lab'                    => $no_lab[$i],
     'low_sampling'              => $low_sampling[$i],
     'sampling_point'            => $sampling_point[$i],
     'sampling_tgr'              => $sampling_tgr[$i],
     'sampling_clk'              => $sampling_clk[$i],
     'sampling_analys'           => $sampling_analys[$i],
     'shift'                     => $shift[$i]
 );

And in my View i've set "set_value('array[]')", like this:

<td>
   <input type="text" name="no_lab[]" id="no_lab" value="<?php echo set_value('no_lab[]'); ?>" required/></td>
<td>
   <input type="text" name="low_sampling[]" id="low_sampling" value="<?php echo set_value('low_sampling[]'); ?>"/></td>
<td>
   <input type="text" name="sampling_point[]" id="sampling_point" size="17" value="<?php echo set_value('sampling_point[]'); ?>"/></td>
<td>
   <input type="text" name="sampling_tgr[]" id="sampling_tgr" value="<?php echo set_value('sampling_tgr[]'); ?>"/></td>
<td>
   <div class="input-group clockpicker col-sm-12" data-autoclose="true" data-time-format="HH:mm">
     <input type="text" name="sampling_clk[]" id="sampling_clk" value="<?php echo set_value('sampling_clk[]'); ?>" required/>
       <span class="input-group-addon">
       <span class="glyphicon glyphicon-time"></span></span>
   </div>
</td>
<td>
   <input type="text" name="sampling_analys[]" id="sampling_analys" value="<?php echo set_value('sampling_analys[]'); ?>"/></td>

reference to CI re-populating form: https://www.codeigniter.com/user_guide/libraries/form_validation.html

So, am i doing it wrong? i'm new to CI.

Upvotes: 0

Views: 2839

Answers (3)

nathaninthecloud
nathaninthecloud

Reputation: 21

I had a similar problem when using an array as the name of a form field and then supplying it as an array to the set_value function. I was unable to re-populate each form field with its corresponding value.

Building off Vigneswaran's suggestion of assigning the result of the set_value function to a variable, I then used the loop's counter as the array key to access each value in the form fields.

for($i = 0; $i < 2; $i++) {
    $a = set_value('ingredients['.$i.']');
    echo form_input('ingredients[]', $a, TRUE);
}

Output with example value attributes:

<input type="text" name="ingredients[] value="form_input_1">
<input type="text" name="ingredients[] value="form_input_2">

Upvotes: 0

AldoZumaran
AldoZumaran

Reputation: 572

just use

<?php echo set_value('no_lab','default value'); ?>

//get the first element of array
echo '<input name="no_lab" id="no_lab" class="no_lab" value="'.$no_lab[0].'" >';
// OR
echo '<input name="no_lab" id="no_lab" class="no_lab" value="'.$no_lab.'" >';

foreach ($no_lab as $nl){
    echo '<input name="no_lab['.$nl.']" id="no_lab_'.$nl.'" class="no_lab" value="'.$nl.'" >';
}

Upvotes: 0

Vigneswaran S
Vigneswaran S

Reputation: 2094

Print_r of your array returns a value. so make your $a=set_value('no_lab[]');. hence $a is array. I think count($a) returns 1, therefore i am using echo $a[0]; now try the code

<td>
<input type="text" name="no_lab[]" id="no_lab" value="<?php $a=set_value('no_lab[]'); echo $a[0]; ?>" required/></td>
<td>
<input type="text" name="low_sampling[]" id="low_sampling" value="<?php  $a=set_value('low_sampling[]');echo $a[0];  ?>"/></td>
<td>
<input type="text" name="sampling_point[]" id="sampling_point" size="17" value="<?php $a= set_value('sampling_point[]');echo $a[0];  ?>"/></td>
<td>
<input type="text" name="sampling_tgr[]" id="sampling_tgr" value="<?php $a= set_value('sampling_tgr[]'); echo $a[0]; ?>"/></td>
<td>
<div class="input-group clockpicker col-sm-12" data-autoclose="true" data-time-format="HH:mm">
<input type="text" name="sampling_clk[]" id="sampling_clk" value="<?php $a= set_value('sampling_clk[]');echo $a[0];  ?>" required/>
<span class="input-group-addon">
<span class="glyphicon glyphicon-time"></span></span>
</div>
</td>
<td>
<input type="text" name="sampling_analys[]" id="sampling_analys" value="<?php $a=set_value('sampling_analys[]');echo $a[0];  ?>"/></td>

Upvotes: 1

Related Questions