2017-02-28 51 views
3

我使用複選框來獲取和存儲用戶的興趣。PHP - MySql:如何使用其他選項存儲和檢索複選框值

enter image description here

存儲這些數據在mysql數據庫與逗號分隔

enter image description here

我的PHP代碼retrive這些數據,並顯示在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()」按照數據庫值將默認值選中複選框。但如何檢查 「其他」(檢查附加圖像)選項並顯示它的值?其中 條件我需要在這裏添加?如何在一個領域的「利息」 存儲在數據庫

+0

結果是什麼print_r($ result)? – paranoid

+1

在你的表格中有第二行的「other」。那麼用戶填寫的複選框或文本框的值是多少? –

+0

@paranoid,更新了問題。請檢查 –

回答

0

您可以:分隔符插入數據。然後顯示數據嘗試以下代碼:

例如:存儲的數據是「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> 
+0

適用於我的一個解決方案,一條規則:**必須在興趣字段末尾存儲「其他」選項** –

+0

謝謝您的解決方案..! :) –

相關問題