【问题标题】:How to check or uncheck the radio button according to value in php?如何根据php中的值选中或取消选中单选按钮?
【发布时间】:2021-07-05 14:59:41
【问题描述】:

我被困在这个问题上。 我有两个单选按钮。一个是Buyer,第二个是Seller

 <input type="radio" name="account_type" value="1" id="acc_buyer" checked="checked">Buyer
 <input type="radio" name="account_type" value="2" id="acc_seller">Seller

Buyer 单选按钮已被选中。

现在我要做的是,我有下面的代码,如果$account_type==2 那么我必须选择seller 单选按钮并取消选中买家。

我试过下面的代码

 if(isset($_SESSION['error'])){
    $error = $_SESSION['error'];
    $account_type = $_SESSION['account_type'];
    unset($_SESSION['account_type']);
    unset($_SESSION['error']);
}
 
 <input type="radio" name="account_type" value="1" id="acc_buyer" checked="checked"> Buyer
 <input type="radio" name="account_type" value="2" id="acc_seller" <?php if(isset($account_type) && $account_type=='2') { ?> checked="checked" <?php }?>> Seller

【问题讨论】:

  • 您在两个单选按钮上都放置了checked。如果它们依赖于一个值,那么它们都应该基于一个条件输出。
  • @El_Vanja,你是对的,但我无法取消选中买家。你能帮我解决这个问题吗?

标签: php session radio


【解决方案1】:
<?php
if(isset($_SESSION['error'])){
    $error = $_SESSION['error'];
    $account_type = $_SESSION['account_type'];
    unset($_SESSION['account_type']);
    unset($_SESSION['error']);
}
?> 
<input type="radio" name="account_type" value="1" id="acc_buyer" checked="checked">
 <input type="radio" name="account_type" value="2" id="acc_seller" <?php if($account_type==2){echo 'checked="checked"';}?>>

【讨论】:

  • 我没有从任何地方得到 $account_type==1。
  • 您可以相应地替换值。但这是基本的工作示例
  • 这有可能在两台收音机上使用checked=checked
【解决方案2】:

这是添加empty()检查的更正。


$checked = [];
$checked[0] = empty($account_type) || (isset($account_type) && $account_type == 1) ? 'checked' : '';
$checked[1] = isset($account_type) && $account_type == 2 ? 'checked' : '';

<input type="radio" name="account_type" value="1" id="acc_buyer" checked="<?php echo $checked[0] ?>">
<input type="radio" name="account_type" value="2" id="acc_seller" checked="<?php echo $checked[1] ?>">

【讨论】:

  • 让我检查一下。给我时间
  • 我检查过,当我重新加载页面时,我会让卖家检查。应该是买家
  • 这是正确的吗? 1) 在新页面加载时,是否应默认选择买家? 2)如果页面加载后,用户点击seller,然后提交,应该选择seller?
  • 不,1分是正确的。对于 2,如果会话已激活且 account_type 为 2,则选择卖家,否则选择买家。
  • 我在重新加载页面时仍然选择了卖家。
【解决方案3】:

我希望我不要重复别人的答案并且我的解决方案不是不必要的,但是我使用了这个代码sn-p:

<?php
if(isset($_SESSION['error']))
{
    $error = $_SESSION['error'];
    $account_type = $_SESSION['account_type'];
    unset($_SESSION['account_type']);
    unset($_SESSION['error']);
}
$account_type_set = isset($account_type);
$buyer = $account_type_set ? ($account_type == 2 ? 0 : 1) : null;
$seller = $account_type_set ? !$buyer : null;
?>
 <input type="radio" name="account_type" value="1" id="acc_buyer" <?php if($buyer) { ?> checked="checked" <?php }?>> Buyer
 <input type="radio" name="account_type" value="2" id="acc_seller" <?php if($seller) { ?> checked="checked" <?php }?>> Seller

【讨论】:

    【解决方案4】:

    我找到了答案:我做了什么,我正在检查 $account_type 是否为空,然后添加检查。所以我要让买家检查一下。

    我不知道这是否是最佳答案,但它解决了我的问题。

    我试过了

    if(isset($_SESSION['error'])){
            $error = $_SESSION['error'];
            $account_type = $_SESSION['account_type'];
            unset($_SESSION['account_type']);
            unset($_SESSION['error']);
        }
    
    <input type="radio" name="account_type" value="1" id="acc_buyer" <?php if(empty($account_type)){?> checked="checked" <?}?>>Buyer
    
    <input type="radio" name="account_type" value="2" id="acc_seller" <?php if( isset($account_type) && $account_type=='2'){?> checked="checked" <?}?>>  Seller
    

    【讨论】:

    • 这基本上就像我提供的第一个解决方案......幸好它对你有用
    • 您的答案中缺少 empty($account_type)。请更新您的答案。我会接受你的回答。
    • 答案已修改为包含empty() 检查。谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 2011-01-08
    • 2011-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多