I am using code igniter and I have an array which coming from a java script function in view file and I want to get it to a form to save in the db after submitting data.After submitting it is going to controller ->model function using post and then it save after some if conditions.Here my question is how we can get that array into some input fields to pass in post method and how we have to define it.
In view file function
function my_function{
//after some codes
const my_array =[];
}
the output of this array is
0:{a:'1',b:'2',c:'35'}
1:{a:'2',b:'3',c:'34'}
2:{a:'5',b:'1',c:'87'}
3:{a:'4',b:'3',c:'90'}
it should come here in readonly mode
<div class="row">
<div class="col-md-5">
<input type="text" class ="a" id="a" name= "a" class="form-control name-pull-image">
</div>
<div class="col-md-5">
<input type="text" class ="b" id="b" name= "b" class="form-control name-pull-image">
</div>
<div class="col-md-5">
<input type="text" class ="c" id="c" name= "c" class="form-control name-pull-image">
</div>
</div>
And this should be save in db as separate records after clicking submit.
How I can implement this? and how i can pass this to the controller?to take it to the model
what exactly your model needed?? I got your question, but am not sure to answer, it's might be wrong. But how about this:
Your html form must be like this, to pass an array to the controller:
<div class="row">
<div class="col-md-5">
<input type="text" class ="a" id="a" name= "a[]" value="a" class="form-control name-pull-image">
</div>
<div class="col-md-5">
<input type="text" class ="b" id="b" name= "b[]" value="b" class="form-control name-pull-image">
</div>
<div class="col-md-5">
<input type="text" class ="c" id="c" name= "c[]" value="c" class="form-control name-pull-image">
</div>
</div>
And get the array when form submitted:
<?php
// your controller...
public function formSubmit() {
$form_data = [];
for ( $i=0; $i < count($this->input->post('a')); $++ ) {
array_push($form_data, [
'a' => $this->input->post('a')[$i],
'b' => $this->input->post('b')[$i],
'c' => $this->input->post('c')[$i],
]);
}
// result
print_r($form_data);
}
// other code...
?>