You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Perfex CRM(CodeIgniter)中实现单选按钮值存入数据库求助

Fixing Radio Button Value Saving in Perfex CRM

Your current radio button code has several issues preventing it from working like the dropdown. Here's the corrected implementation, along with explanations of the fixes:

Corrected Radio Button Code

First, keep your existing $accessability array (it's reusable for the radio buttons):

$accessability = [
    0 => ["id" => "Yes", "name" => _l("Yes")],
    1 => ["id" => "No", "name" => _l("No")],
    2 => ["id" => "Not Applicable", "name" => _l("Not Applicable")]
];

Then replace your radio button HTML with this dynamic, clean version:

<div class="col-md-12">                           
    <div class="mtop10">
        <span>ACCESSABILITY TO PROPERTY WAS CLEAR</span>
        <?php foreach ($accessability as $option): ?>
            <div class="radio radio-primary radio-inline">
                <input type="radio" 
                       value="<?php echo esc($option['id']); ?>" 
                       id="accessability_<?php echo esc($option['id']); ?>" 
                       name="accessability" 
                       <?php echo isset($disscussion->accessability) && $disscussion->accessability == $option['id'] ? 'checked' : ''; ?>>
                <label for="accessability_<?php echo esc($option['id']); ?>"><?php echo $option['name']; ?></label>
            </div>
        <?php endforeach; ?>
    </div>
</div>

Key Fixes Explained

  • Shared Name Attribute: All radio buttons use name="accessability" (matching your dropdown's name). This groups them together, ensuring only one can be selected at a time.
  • Matching Values: Each radio's value matches the dropdown's id values ("Yes", "No", "Not Applicable"), so the saved data is consistent with your existing dropdown logic.
  • Valid Checked Condition: The PHP checks if the saved value ($disscussion->accessability) matches the radio's value, adding the checked attribute when true.
  • Unique IDs: Each radio gets a unique ID (e.g., accessability_Yes) so the <label> elements correctly link to their inputs.
  • XSS Protection: Uses CodeIgniter's esc() function to sanitize output, preventing cross-site scripting vulnerabilities.

Why Your Original Code Failed

  • Non-Matching Names: Your radios had different names ("Yes" and "No"), so they weren't grouped—selecting one didn't deselect the other, and the controller wouldn't receive a consistent accessability value.
  • Incorrect Values: Both radios used value="1", which doesn't match your dropdown's values, leading to wrong data being saved.
  • Broken PHP Syntax: The checked condition in your original code was incomplete (missing the if body and proper comparison), so it never set the checked attribute.

Save Logic Note

Since the radio buttons use the same name="accessability" as your dropdown, you don't need to change any backend save logic—your existing CodeIgniter controller code that handles the dropdown submission will work for the radios too.

内容的提问来源于stack exchange,提问作者Rohan Jaiswal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 22:30:48