Reputation: 3615
I have to submit my codeIgniter form which have dynamic columns. how can I get them from controller using a loop.
this is my view page
<?php echo form_open_multipart('main_controller/do_insert');?>
<div id="mainDiv">
<div><input type="text" name="name0"/></div>
<div><input type="file" name="img0"/></div>
</div>
<input type="button" value="add an entry" onClick="add('0')">
<input type="submit" value="save all"/>
<?php from_close();?>
<script>
function add(x)
{
var count=x;count++;
var str1="<div><input type='text' name='name"+count+"'/></div>"
var str2="<div><input type='file' name='img"+count+"'/></div>"
var str3="<input type='button' value='add an entry' onClick='add(`1`)'>";
$("#mainDiv").append(str1+str2+str3);
}
</script>
note: the value of 'x' may not work properly but its not relevant here, can fix it by a global variable.
My problem is that how can i code controller for getting all the values including images and save them to table, while submitting the form.
Upvotes: 0
Views: 374
Reputation: 2375
my suggestion, use file name as array field like.
<?php echo form_open_multipart('main_controller/do_insert');?>
<div id="mainDiv">
<div class='group'>
<div><input type="text" name="name[]"/></div>
<div><input type="file" name="img[]"/></div>
</div>
</div>
<input type="button" id="add an entry">
<input type="submit" value="save all"/>
<?php from_close();?>
Script look like..
<script>
$('#add an entry').on('click', function() {
var cloneRow = $('.group').clone();
cloneRow.find("input").val("").end();
cloneRow.appendTo('#mainDiv');
</script>
then in controller:
foreach($this->input->post() as $post){
//do action on your data...here
}
foreach($_FILES['userfile'] as $key => $value)
{
//images data
}
Upvotes: 1
Reputation: 96
$i = 0;
foreach($this->input->post() as $post) {
echo $post; //Or echo $this->input->post('name'.$i);
print_r($_FILES['img'.$i]);
$i++;
}
Upvotes: 1