【问题标题】:PHP - MySql : How to store and retrieve checkbox value with other optionPHP - MySql:如何使用其他选项存储和检索复选框值
【发布时间】:2017-02-28 15:01:19
【问题描述】:

我使用复选框来获取和存储用户的兴趣。

将这些数据存储在数据库mysql中,用逗号分隔

我的 php 代码用于检索这些数据并显示在 html 页面中

<?php

....

// Get data
$result = profile($uname);
$result['interest'] = explode(',', $result['interest']);

print_r($result['interest']); // output: Array ( [0] => option1 [1] => option2 [2] => option3 )

?>

....

<div class="col-sm-6">
    <div class="form-check checkbox-style">
        <label class="label-weight">Interests</label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]" onclick="return interest_validate(this);" id="selectall"><span class="check-label-pad"> Select all</span></label></label><label for="" class="opacity-set label-set" id="select-label"></label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]" onclick="return option_select(this);" value="option1" <?php echo in_array('option1',$result['interest']) ? 'checked' : ''; ?>><span class="check-label-pad">Option 1</span></label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]" onclick="return option_select(this);" value="option2" <?php echo in_array('option2', $result['interest']) ? 'checked' : ''; ?>><span class="check-label-pad">Option 2</span></label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]" onclick="return option_select(this);" value="option3" <?php echo in_array('option3', $result['interest']) ? 'checked' : ''; ?>><span class="check-label-pad">Option 3</span></label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]" id="other_checkbox" onclick=" return other_validate(this);" value="other"><span class="check-label-pad"  >Other</span> &nbsp;&nbsp;</label><input type="text " class="text-style-other opacity-set" id="other-text" onblur="return other_text_validate();" />
        <label for="" class="opacity-set label-set" id="other-text-label"></label>
    </div>
</div>

问题:在编辑模式下,我使用“in_array()”根据数据库值默认选中复选框。但是如何检查 “其他”(检查附图)选项并显示它的值?哪个 我需要在这里添加条件吗?如何在一个字段中存储“兴趣” 在数据库中

【问题讨论】:

  • 什么是结果 print_r($result)?
  • 在您的表格中,第二行有 other。那么它是用户填写的复选框或文本框的值吗?
  • @paranoid,更新的问题。请检查
  • @B.Desai,是的,这是解决这个问题的一种选择
  • 您可以插入文本框值,其中所有值都用任何 caharecter 分隔,例如 : 示例:您的字段应该是:option2,option3,other:xyz。其中 xyz 是 yoyr 文本框数据

标签: php mysql arrays checkbox


【解决方案1】:

您可以使用: 分隔符插入数据。然后显示数据尝试以下代码:

示例:存储数据为“option2,option3,other:xyz”;

<?php
$result['interest']="option2,option3,other:xyz";
$result['interest']=explode(":", $result['interest']);
$txt_value="";
if(count($result['interest']) >= 2)
  $txt_value = $result['interest'][1];  

$result['interest']=explode(",",$result['interest'][0]); 


?>
<div class="col-sm-6">
    <div class="form-check checkbox-style">
        <label class="label-weight">Interests</label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]"  id="selectall"><span class="check-label-pad"> Select all</span></label></label><label for="" class="opacity-set label-set" id="select-label"></label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]" value="option1" <?php echo in_array('option1',$result['interest']) ? 'checked' : ''; ?>><span class="check-label-pad">Option 1</span></label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]"  value="option2" <?php echo in_array('option2', $result['interest']) ? 'checked' : ''; ?>><span class="check-label-pad">Option 2</span></label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]" value="option3" <?php echo in_array('option3', $result['interest']) ? 'checked' : ''; ?>><span class="check-label-pad">Option 3</span></label><br>
        <label class="form-check-label label-weight">
            <input type="checkbox" class="form-check-input" name="interest[]" id="other_checkbox" <?php echo in_array('other', $result['interest']) ? 'checked' : ''; ?>><span class="check-label-pad"  >Other</span> &nbsp;&nbsp;</label><input type="text " class="text-style-other opacity-set" value="<?=$txt_value?>" id="other-text"/>
        <label for="" class="opacity-set label-set" id="other-text-label"></label>
    </div>
</div>

【讨论】:

  • 对我来说完美的解决方案有一条规则:必须在感兴趣的字符串末尾存储“其他”选项。
  • 感谢您的解决方案..! :)
猜你喜欢
  • 1970-01-01
  • 2013-06-20
  • 1970-01-01
  • 2012-12-13
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
  • 1970-01-01
相关资源
最近更新 更多