I need to get post value as array from form created using for loop ,
Here is my view
<?php for ($i=0;$i<5;$i++) {?>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<input name="<?php echo 'fname'.$i?>" class="form-control" type="text" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Surname</label>
<input name="<?php echo 'sname'.$i?>" class="form-control" type="text" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Date of Birth</label>
<input name="<?php echo 'dob'.$i?>" class="date-pick-years form-control" type="text" />
</div>
</div>
</div>
<?php }?>
<input type="submit" />
How to get these post value in controller , i need as a array for example i value is 2 means i need my output as
$array1=array('name'=>$this->input->post('name0')),'sname'=>$this->input->post('sname0')),'dob'=>$this->input->post('dob0'))
$array2=array('name'=>$this->input->post('name1')),'sname'=>$this->input->post('sname1')),'dob'=>$this->input->post('dob1'))
Total array =array($array1,$array2);
How to get this in a loop in controller to get required output
If you want to get an array of values that belong semantically to the same name
value, use []
in your html:
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<input name="fname[<?=$i?>]" class="form-control" type="text" />
</div>
</div>
Then use the usual $data = $this->input->post()
in your controller to read out the data. The data should be in this format:
$post[
fname [
0 => 'name1',
1 => 'name2',
...
],
sname [
0 => 'sname1',
1 => 'sname2',
...
],
...
]
]
$resultArray = array();
for($i = 0; $i < <formlength>; $i++) {
$resultArray[$i]['fname'] = $post['fname'][$i];
$resultArray[$i]['sname'] = $post['sname'][$i];
...
}
Try this:
<?php for ($i = 0; $i < 5; $i++) { ?>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<input name="data[item<?php echo $i; ?>][<?php echo 'fname' . $i ?>]" class="form-control" type="text"/>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Surname</label>
<input name="data[item<?php echo $i; ?>][<?php echo 'sname' . $i ?>]" class="form-control" type="text"/>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Date of Birth</label>
<input name="data[item<?php echo $i; ?>][<?php echo 'dob' . $i ?>]" class="date-pick-years form-control" type="text"/>
</div>
</div>
</div>
<?php } ?>
You will get:
$_POST['data'] = array (
'item0' =>
array (
'fname0' => '',
'sname0' => '',
'dob0' => '',
),
'item1' =>
array (
'fname1' => '',
'sname1' => '',
'dob1' => '',
),
'item2' =>
array (
'fname2' => '',
'sname2' => '',
'dob2' => '',
),
'item3' =>
array (
'fname3' => '',
'sname3' => '',
'dob3' => '',
),
'item4' =>
array (
'fname4' => '',
'sname4' => '',
'dob4' => '',
),
);
If you want to use them in variables you can extract it:
$data = $this->input->post('data');
if(is_array($data)) {
extract($data);
if(isset($item1)) {
var_export($item1);
// it will contains:
// array (
// 'fname1' => '',
// 'sname1' => '',
// 'dob1' => '',
// );
}
}
use this code in your html code than you get the right result as per your requirement.
<?php for ($i=0;$i<5;$i++) {?>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label>Name</label>
<input name="name[<?=$i?>][fname]" class="form-control" type="text" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Surname</label>
<input name="name[<?=$i?>][sname]" class="form-control" type="text" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label>Date of Birth</label>
<input name="name[<?=$i?>][dob]" class="date-pick-years form-control" type="text" />
</div>
</div>
</div>
<?php }?>
<input type="submit" />
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.