2015-05-12 160 views
1

我在wordpress中工作。我從數據庫中提取數據並在下拉菜單中顯示。 而我的代碼是這樣的。複選框不顯示下拉值

<select multiple="multiple" class="tole_int"> 
    <?php 
    global $wpdb; 
    $query_interest = "select p.* from wp_posts as p where p.post_type = 'interests' and p.post_name !='' "; 
    $tolerancetypes = $wpdb->get_results($query_interest,OBJECT); 

    foreach($tolerancetypes as $key=>$interest) 
     { 

    ?> 
    <option value="<?php echo $interest->ID; ?>"><?php echo ucfirst($interest->post_name); ?></option> 
    <?php 
       } 
     ?> 
</select> 

我寫了multiple =「multiple」屬性來選擇多個值。但我想添加複選框和值。那我該寫些什麼呢?

回答

0

確保您要添加的複選框中Form,如果你想使用這metabox無需創建Form

因此,更改selectboxcheckbox

<?php 
global $wpdb; 
$query_interest = "select p.* from wp_posts as p where p.post_type = 'interests' and p.post_name !='' "; 
$tolerancetypes = $wpdb->get_results($query_interest,OBJECT); 
foreach($tolerancetypes as $key=>$interest) 
    { 

?> 
     <input type="checkbox" name="tole_int[]" value="<?php echo $interest->ID; ?>" /><?php echo $interest->post_title; ?> 
    <?php 
      } 

並提交你的表單時可以得到逗號分隔的值並保存

$tole_int = implode(",", $_POST['tole_int']); 

或者如果你想檢查多個複選框值的輸出嘗試

echo '<pre>';print_r($_POST['tole_int']);echo '</pre>';