Nisarg Bhavsar
Nisarg Bhavsar

Reputation: 956

Checkbox is not display with dropdown values

I am working in wordpress.I have fetched data from database and display in a dropdown. And my code is like this.

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

I have written multiple="multiple" property for selecting multiple values.But I want to add check box along with the values. So what should I have to write?

Upvotes: 1

Views: 52

Answers (2)

Noman
Noman

Reputation: 4116

Make sure you are adding the checkbox in Form, if you want to use this in metabox no need to create Form

So, Change selectbox to checkbox

<?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
            }

And when submitting the form you can get the values seprated by comma and save

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

or if you want to check the output of multiple checkbox values try

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

Upvotes: 0

ajaykumartak
ajaykumartak

Reputation: 776

Try this "Bootstrap Multiselect"

http://davidstutz.github.io/bootstrap-multiselect/

Then you don't need to write checkbox code manually.

Upvotes: 1

Related Questions