gpt4 book ai didi

PHP-MySql : How to store and retrieve checkbox value with other option

转载 作者:可可西里 更新时间:2023-11-01 09:00:59 24 4
gpt4 key购买 nike

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

enter image description here

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

enter image description here

我的 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>

Issue : in edit mode, I am using "in_array()" for to give as default checked to checkbox as per database value. But how to check "Other" (check attached image) option and display it's value ? which condition i need to add here? how to store in one field "interest" in database

最佳答案

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

例子:存储的数据是“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>

关于PHP-MySql : How to store and retrieve checkbox value with other option,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42501358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com