自学的小问题,帮忙看看

WBOY
Release: 2016-06-23 13:37:51
Original
867 people have browsed it

为什么有数据 checkbox 无法选中呢?哪里的问题 高手帮忙看看






<?phpmysql_connect('localhost','root','root');mysql_select_db('ceshi');mysql_query('set names utf8');$sql="select * from test where username='小丁'";$result=mysql_query($sql);$row=mysql_fetch_assoc($result);$a=$row['love'];$arr=explode('|', $a);$ah=array('打麻将','看电影','英雄联盟','睡觉');?>用户名:<input type="text" name="username" value="<?php echo $row['username'] ?>"/><br>爱好:<?php          for ($i=0;$i<count($ah);$i++):            $sled='';            if(in_array($ah[i],$arr)){                    $sled='checked';            }    ?>    <input type="checkbox" <?php echo $sled ?>><?php echo $ah[$i] ?>    <?php    endfor;    ?>
Copy after login


回复讨论(解决方案)

先看生成的html是否正确

F12   看看你的页面里生成的html是啥样的! 往往是你觉得自己写的对,但是就是有错误!

F12   看看你的页面里生成的html是啥样的! 往往是你觉得自己写的对,但是就是有错误!



先看生成的html是否正确



var_dump($arr);

看看是什么

看HTML貌似没得问题,是不是还要加其他的属性

var_dump($arr);

看看是什么



array(3) { [0]=> string(9) "打麻将" [1]=> string(9) "看电影" [2]=> string(6) "睡觉" }

在判断之前要设置
  $sled=''; 
变量名为空才对

那 var_dump($ah); 又看到的是什么?

那 var_dump($ah); 又看到的是什么?



array(4) { [0]=> string(9) "打麻将" [1]=> string(9) "看电影" [2]=> string(12) "英雄联盟" [3]=> string(6) "睡觉" } 

在判断之前要设置
  $sled=''; 
变量名为空才对



是空的呀 还不是没用

if(in_array($ah[i],$arr)){

if(in_array($ah[ $i],$arr)){

原来是这个问题!
if(in_array($ah[ i],$arr)){
少了 $
应为
if(in_array($ah[ $i],$arr)){

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!