drc4925 2016-09-27 07:08
浏览 56
已采纳

如何将复选框值带到codeigniter中更新页面

Actually i fetched data successfully but checkbox is not showing checked values. Maybe the logic is wrong. And it is necessary to use foreach here, can't we direct echo in value field, i am new to it and maybe the mistakes are silly

view

<html>
<?php
foreach($post_id as $data){

     $a=$data->id;
     $b=$data->name;
     $c=$data->email;
     $d=json_decode($data->skills);
     $e=$data->notes;
     $f=$data->gender;
}
?>
<?php var_dump($d);?>
<?php var_dump($f);?>
<body>
<form method="post" action="<?php echo site_url('Student_info/update'); ?>">
<table>
<tr>
<td>ENTER NAME</td>
<td><input type="text" name="name" value="<?php echo $b;   ?>"></td>
</tr>
<tr>
<td>ENTER EMAIL </td>
<td><input type="email" name="email" value="<?php echo $c; ?>"></td>
</tr>
<tr>
<td>ENTER SKILLS</td>
<td>
<input type="checkbox" name="skills[]" value="php" <?php if($d == "php"){echo 'checked="checked"'; } ?>>php<br>
<input type="checkbox" name="skills[]" value="dotnet" <?php if($d == "dotnet"){echo 'checked="checked"'; } ?> >dotnet<br>
<input type="checkbox" name="skills[]" value="java" <?php if($d == "java"){echo 'checked="checked"'; } ?>>java<br>
<input type="checkbox" name="skills[]" value="ruby_on_rails" <?php if($d == "ruby_on_rails"){echo 'checked="checked"'; } ?> >ruby_on_rails<br>
</td>
</tr>
<tr>
<td>NOTES</td>
<td> <textarea name="notes" rows="4" cols="50"><?php echo $e; ?></textarea> </td>
</tr>
<tr>
<td>GENDER</td>
<td> <input type="radio" name="gender" value="male" <?php if($f == "male"){echo 'selected="selected"'; } ?>> Male<br>
     <input type="radio" name="gender" value="female" <?php if($f == "Female"){echo 'selected="selected"'; } ?>> Female<br> </td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="add" value="submit"/></td>    
</tr>  
</table>
</form>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • duanla3319 2016-09-27 07:32
    关注

    Result of this $d is an object not a string value because you are using json_decode() here:

    $d=json_decode($data->skills); // this will produce an object
    

    You can use in_array(), but for this you need to use second param as TRUE in json_decode() function, this will return the result in array, something like:

    $d=json_decode($data->skills,TRUE); // this will return an array
    

    Than you can check like that:

    <input type="checkbox" name="skills[]" value="php" <?=(in_array("php",$d) ? 'checked=""' : '')?>>php
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵