2015-05-12 124 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>';