在Perfex CRM(CodeIgniter)中实现单选按钮值存入数据库求助
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
idvalues ("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 thecheckedattribute 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
accessabilityvalue. - 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
ifbody and proper comparison), so it never set thecheckedattribute.
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
相关产品推荐
相关产品推荐

