【问题标题】:how to check or select the radio button based from the data from the db table?如何根据 db 表中的数据检查或选择单选按钮?
【发布时间】:2011-08-26 20:17:31
【问题描述】:

我遇到了问题,我的应用似乎没有根据保存的数据检查单选按钮。实际上我有两个单选按钮,它的值为 1 或 2 ,值的保存确实有效,但是当用户使用他/她的帐户再次访问该应用程序时,没有选中任何单选按钮

这是从 db 表中提取单选按钮数据的代码

//User.php
public function getGalleryMenuStyle($id = ''){
    $gallerymenustyle = "SELECT gallery_menu_style FROM users WHERE user_id = ".$id;
    $this->dbSelectQuery = $gallerymenustyle;
    $checkedgallerymenustyle = $this->userDBSelect();
       if($checkedgallerymenustyle){
           return $checkedgallerymenustyle;
       } else {
           return false;
       }
}

这是前端的代码

$userCats = new User();
$user_id = $_SESSION['userData']['user_id'];
$checkedgallerymenustyle = $userCats->getGalleryMenuStyle($user_id);

    <div align="center" class="radio_group">
        <input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="1" <?php if($checkedgallerymenustyle == 1){ echo "selected"; }?> /> Gallery Link - In the navigation of my website, display one "gallery" link<br />
        <input type="radio" id="gallerymenustyle2" class="element radio" name="gallerymenustyle[]" value="2" <?php if($checkedgallerymenustyle == 2){ echo "selected"; }?> /> Category Links - In the navigation of my website, display a separate link to each category.
    </div>

如何解决这个问题?我的代码有什么问题吗?

【问题讨论】:

  • 您是否尝试在前端回显 $checkedgallerymenustyle 以查看您是否获得了正确的值?
  • 好的,所以不要使用echo,试试print_r($checkedgallerymenustyle);,这将显示你的数组中有什么,你可以看看它们是否有你需要的值。

标签: php mysql


【解决方案1】:

单选按钮使用“选中”而不是“选中”

【讨论】:

  • 尝试删除“if(...)”然后在第一个。如果它显示为已选中,则您的值不在预期范围 [1,2] 内,但如果不是,则您的检查启用脚本是错误的。
【解决方案2】:

这样做

 <div align="center" class="radio_group">
        <input type="radio" id="gallerymenustyle1" class="element radio" name="gallerymenustyle[]" value="1" <?php if($checkedgallerymenustyle == 1){ echo "checked=\"yes\""; }?> /> Gallery Link - In the navigation of my website, display one "gallery" link<br />
        <input type="radio" id="gallerymenustyle2" class="element radio" name="gallerymenustyle[]" value="2" <?php if($checkedgallerymenustyle == 2){ echo "checked=\"yes\""; }?> /> Category Links - In the navigation of my website, display a separate link to each category.
    </div>

checked="是"

【讨论】:

  • 它必须而且我认为你返回的是 true 而不是 1 检查。
猜你喜欢
  • 2019-09-16
  • 2017-06-30
  • 1970-01-01
  • 1970-01-01
  • 2013-03-13
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
相关资源
最近更新 更多