dttphb59319
2017-04-18 04:36
浏览 40
已采纳

如何在codeigniter中获取值数组?

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];
}

图片转代码服务由CSDN问答提供 功能建议

如何在codeigniter中获取值数组? 当我得到值post post数组并回显该值时,我遇到了问题。 如何在提交时显示帖子值? 错误信息:

 遇到PHP错误
 
Severity:Notice 
 
消息:未初始化的字符串偏移量:0 \  n 
文件名:controllers / blablabla 
   
 
 

查看html:

 &lt;?php $ i = 0;  foreach($ doc as $ row){?&gt; 
&lt; label&gt; 
&lt; input name =“size [&lt;?php echo $ i;?&gt;]”type =“checkbox”value =“&lt;?  php echo $ row ['doc'];?&gt;“&gt;&amp; nbsp;&amp; nbsp;&lt;?php echo $ row ['doc'];  ?&gt; 
&lt; / label&gt; 
&lt;?php $ i ++;  }&gt; 
   
 
 

控制器:

  $ size = $ this-&gt; input-&gt; post  ('size'); 
for($ i = 0; $ i&lt; count($ doc); $ i ++)
 {
 echo $ size [$ i]; 
} 
   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongqiang5865 2017-04-18 05:03
    已采纳

    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;
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douju9847 2017-04-18 04:59

    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;
    }
    
    评论
    解决 无用
    打赏 举报
  • dtueufe82643 2017-04-18 05:08

    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];
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题