简体   繁体   中英

How to get the selected multiple values from checkbox and submit them in a form to get the matched search results

This form contains a dropdown list that has multiple options with checkboxes, now when a user selects 1 or more checkboxes and presses submit, the values should be processed with the matching search results. Previously, I had a select tag with an option tag that was used to get the results. Now, since I added the functionality of searching for more than one option, I added checkboxes, but now I am not sure how to get the results.

Html and Php Code

<form action="<?php the_permalink() ?>" class="ajax-search-form leaders-header" id="searchform" method="post" role="search">
        <?php get_template_part( 'blocks/leaderboard/search-form' ) ?>
        <div class="sort">

        <body>
        <?php if ($departments = get_field_object('field_5da06da87ae0f')) : ?>
        <div class="sort-item">
        <div id="list1" class="dropdown-check-list" tabindex="100">
            <span class="anchor" style="font-size: small">All Departments</span>
            <ul id="items" class="items" name="department" >
                <label for="test">
                <?php foreach ($departments['choices'] as $key => $department) : ?>
                    <input type="checkbox" name="check" id="test" value="unchecked" /> <option style="font-size: small" <?php if ( isset($_REQUEST['role']) and $_REQUEST['role'] == $key) echo 'selected' ?> value="<?php echo $key; ?>">

                        <?php echo $department; ?></option></label></br>

                <?php endforeach; ?>

                <input style="font-size: small" type="submit" value="Submit" "/>
            </ul>
        </div>
        </div>
        <?php endif ?>
        </body>

JavaScript:

<script type="text/javascript">

            var checkList = document.getElementById('list1');
            var items = document.getElementById('items');
            var isChecked = document.getElementById('items').checked;
            checkList.getElementsByClassName('anchor')[0].onclick = function (evt) {
                if (items.classList.contains('visible')){
                    items.classList.remove('visible');
                    items.style.display = "none";
                }
                else{
                    items.classList.add('visible');
                    items.style.display = "block";
                }

            }

            items.onblur = function(evt) {
                items.classList.remove('visible');
            }


        </script>

Problem is that every checkbox will have the same name and id so you will be not able to recognize which option belongs to which checkbox but you can try something like this

<?php foreach ($departments['choices'] as $key => $department) : ?>
                    <input type="checkbox" name="<?= $department ?>" id="<?= $department ?>" value="unchecked" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM