Get the selected option from drop down list

I created a dropdown list using data from the db to fill the options. This is working fine. Now I want to get the chosen value to select all posts with that value from the db. I have been searching and trying many suggestions I found, but until now nothing seems to be doing the trick. If possible I want to get this done in php (value needs to be in $ to build db-query).

This is the code that outputs the select-part:

div class="search-bar-container"
    div class="drop-down-course"
        select class="course" name="course"
            option disabled selected value="0" - gang - /option
            ?php
            // Get all the options from the database for the 'course' drop down list.
            $my_course = $wpdb - get_results( "SELECT term_id, name FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = 'wprm_course') " )  ; 
                if (!empty ($my_course ) ) {
                    foreach ( $my_course as $my_course ) {
                        echo 'option value ="' . $my_course - term_id . '"' . $my_course - name . '/option';
                    }
                }
            ?
        /select
    /div
/div

I've read about using the selected() function, but I can't get that working for me. If I understand correctly I need a conditional selected="selected" in the option part. If anyone could help me with this I would appreciate it.

Edit: I've managed to get the value using jquery (I want a solution without a submit button). The code:

    $(document).ready(function(){
    $("#course").change(function(){
     var selectedcourse = $('#course').val(); 
    });
    });

Now I need to get the value into php on a new page. I'm trying to get that to work.

Edit2: I Got the last bit working as wel. The line of code that comes after var selectedcourse = $('#course').val(); is:

window.location.href = 'http://' + window.location.hostname + '/wordpress/zoekresultaten?course=' + selectedcourse;

In wp admin I created a page called zoekresultaten and with the page-atributes I assigned a template to that page to process the selected option. I used a simple echo command on that template and it shows the value of the selected option of the previous page. So now I can move on to process the selection.

Topic dropdown Wordpress

Category Web


I have been searching and trying many suggestions I found, but until now nothing seems to be doing the trick. If possible I want to get this done in php (value needs to be in $ to build db-query).

Your course selection drop-down must be enclosed within a <form> tag with a submit button, like this

    <?php 
       $course = '';
       if(isset($_POST['course'])) $course = $_POST['course'];

          <div class="search-bar-container">

             <form method="POST">

               <div class="drop-down-course">
                  <select class="course" name="course">
                     <option disabled selected value="0"> - gang - </option>
                     <?php
                       // Get all the options from the database for the 'course' drop down list.
                       $my_course = $wpdb -> get_results( "SELECT term_id, name FROM wp_terms WHERE term_id IN (SELECT term_id FROM wp_term_taxonomy WHERE taxonomy = 'wprm_course') " )  ; 
                       if (!empty ($my_course ) ) {
                           foreach ( $my_course as $my_course ) {
                               echo '<option value ="' . $my_course -> term_id . '" ' . selected($my_course -> term_id , $course).'>' . $my_course -> name . '</option>';
                            }
                       }
                ?>
                   </select>
             </div>

             <input type="submit" value="Go">
          </form>

       </div>


    <?php

          // Get posts using $course as your selected option here
    ?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.