2

How to get value post array in codeigniter? I have problem when I get value post array and echo the value. How to show post value when submit? here the error message:

A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 0

Filename: controllers/blablabla

view html:

<?php $i=0; foreach ($doc as $row) { ?>
<label>
<input name="size[<?php echo $i; ?>]" type="checkbox" value="<?php echo $row['doc']; ?>">&nbsp;&nbsp;<?php echo $row['doc']; ?>
</label>
<?php $i++; } ?>

controller :

$size = $this->input->post('size');
for ($i=0; $i<count($doc); $i++) 
{
   echo $size[$i];
}
7
  • 1
    Do this $arr = $this->input->post(); print_r($arr); and check if you are getting, and yes, values will come which are checked only Commented Apr 18, 2017 at 4:38
  • @rahul_m I do that you suggested, just show white screen. Commented Apr 18, 2017 at 4:41
  • Have you kept die(); exit; ? after print_r and obvious it will come after posting form not as direct if you are using same function for loading form and posting form Commented Apr 18, 2017 at 4:42
  • @rahul_m : yes I do, here my code $size = $this->input->post('size'); print_r($size); die(); the result still white screen. Commented Apr 18, 2017 at 4:44
  • No I didn't suggested this, check my comment or copy paste it. Your posted code is wrong Commented Apr 18, 2017 at 4:49

4 Answers 4

4

Change the way name of checkbox written as follows,

<?php foreach ($doc as $row) { ?>
<label>
  <input name="size[]" type="checkbox" value="<?php echo $row['doc']; ?
  >">&nbsp;&nbsp;<?php echo $row['doc']; ?>
</label>
<?php } ?>

And in post method,

$size_arr = $this->input->post('size');
foreach($size_arr as $v){
  echo $v;
}

if for some reason it is not working then check with,

$size_arr = $_POST['size'];
foreach($size_arr as $v){
  echo $v;
}

EDIT

One more alternative,

$arr = $this->input->post();
$size_arr = $arr['size'];
foreach($size_arr as $v){
  echo $v;
}

Core version,

$arr = $_POST;
$size_arr = $arr['size'];
foreach($size_arr as $v){
  echo $v;
}
Sign up to request clarification or add additional context in comments.

9 Comments

thats still not working, I got this message A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach()
check my edit section, if still not working, print_r($arr) and post here, I will look into it
I got more error message A PHP Error was encountered Severity: Notice Message: Undefined variable: size
It means, you are not getting size input values, can you show me $arr values
you sure, form's attribute method is post ?
|
2

Your html form code should be like below.

<input name="size[<?php echo $i; ?>]" type="checkbox" value="<?php echo $row['doc']; ?>"> 

Inside controller your code should be like below.

$size = $this->input->post('size');
foreach($size as $sa)  
{
   echo $sa;
}

1 Comment

Wykar : thats not working, I got error message A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach()
2

No need to use $i in checkbox name in view file just take an array

View file

<?php foreach ($doc as $row) { ?>
<label>
<input name="size[]" type="checkbox" value="<?php echo $row['doc']; ?>">&nbsp;&nbsp;<?php echo $row['doc']; ?>
</label>
<?php  } ?>

Controller

$countsize = count($this->input->post('size'));
for ($i=0; $i<$countsize ; $i++) 
{
   echo $this->input->post('size')[$i];
}

Comments

0

This one works for me

In View file

<div id="area_input">
  <div id="inputan" class="form-inline">
    <div class="form-group col-sm-6">
      <input type="text" class="form-control" name="size[]" placeholder="ukuran">
    </div>  
    <div class="form-group col-sm-6">
      <input type="text" class="form-control" name="size[]" placeholder="ukuran">
    </div>   
    <div class="form-group col-sm-6">
      <input type="text" class="form-control" name="size[]" placeholder="ukuran">
    </div>
    <div class="form-group col-sm-6">
      <input type="text" class="form-control" name="size[]" placeholder="ukuran">
    </div>            
  </div>  
</div> 

you can repeat input as needed.

in Controller file

$data = array(
            'size' => $this->input->post('size'),
        );

You can check stucture of array using print_r($data), or print 'em using:

foreach ($data as $key => $value) {
   foreach ($value as $detail) {
        echo $detail;
        echo "<br>";
    }
}

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.