0

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.

7
  • in value="<?php echo set_value('no_lab[]'); ?>". what will be the value?.//set_value('no_lab[]') ?// can you explain Commented Dec 3, 2015 at 4:44
  • When i fill the forms and submit it, i want to get the value to showing up on View, thats why i set "<?php echo set_value('no_lab[]'); ?>" but as you can see in the pict, i cannot get the values. Commented Dec 3, 2015 at 4:50
  • have you wrote set_value()// function? Commented Dec 3, 2015 at 5:04
  • you are calling set_value() function and passing value as no_lab[].i am right ? can you post your set_value() function Commented Dec 3, 2015 at 5:33
  • set_value() is designed to work with CI form validation, set_value() is not my own function. set_value() can fill the field with a value using parameter. After the form is submitted, you can display the form again and the field will be filled with the value that was submitted. In my case, its didn't work in array sir. Commented Dec 3, 2015 at 6:40

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

4 Comments

by those code i can get the values, but i got a new error " Message: Uninitialized string offset: 0 "
echo $a[0];// replace echo $a; and try
well, with this $a=set_value('no_lab[0]'); echo $a; no error again and i successful get the values, thank you very much sir.
@Billadsys :happy to hear. but you only solved it perfectly. its your work bro :)
0

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.'" >';
}

2 Comments

"default value" ? did you mean array ?
input accept a value not array, or create n inputs <input type="text" name="no_lab[]" id="no_lab" value="<?php $a=set_value('no_lab[0]'); echo $a[0]; ?> <input type="text" name="no_lab[]" id="no_lab" value="<?php $a=set_value('no_lab[1]'); echo $a[1]; ?>, remove the id attribute and use a class
0

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">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.