0

Hello this is my current code:

<select id="link_course">
<option value="">Select Course</option>
<option value="ALL">ALL</option>
<option value="BSIT" selected>BSIT</option>
<option value="BSA">BSA</option>
<option value="BSBA">BSBA</option>
<option value="BSHRTM">BSHRTM</option>
</select>

<script>
    $("#link_course").change(function()
          {
              document.location.href = "home.php?course=" + $(this).val();

        });
</script>

If you click any of those options, for example BSBA, it will go to home.php?course=BSBA, and also you can see that the selected attribute is on the option BSIT. What I wanted to do is whenever I click another link, for example BSHRTM, the selected attribute will go to BSHRTM like it would be <option value="BSHRTM" selected>BSHRTM</option>. Any thoughts?

wobsoriano
  • 12,348
  • 24
  • 92
  • 162

4 Answers4

1

You can do selection via JavaScript :

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

if(getQueryVariable('course') != false) {
    $('#link_course option[value="' + getQueryVariable('course') + '"]').prop('selected', true);
}

Reference

jmgross
  • 2,306
  • 1
  • 20
  • 24
0

After loading the new page, you need to run JavaScript code to set the value on the correct item from the list.

MeanGreen
  • 3,098
  • 5
  • 37
  • 63
  • Since you're already using JQuery, have you tried searching for information about running code on page load? Google is your friend :) – MeanGreen Apr 16 '15 at 14:28
0

Use window.location.hrefto get current URL. Then check on which page is user currently on and finally select corresponding option.

if(window.location.href.indexOf('home.php?course=BSHRTM)') != -1) {
   $('select#link_course option[value=BSHRTM]').attr("selected","selected");
}

To avoid code duplication see Parse query string in JavaScript

Community
  • 1
  • 1
Mateusz Nowak
  • 4,021
  • 2
  • 25
  • 37
0

You should do it in PHP:

<option value="BSHRTM" <?php if($course == 'BSHRTM') echo 'selected';?>>

Supposing that you previsously set $course = $_GET['course'];

oliverpool
  • 1,624
  • 13
  • 30