I used loop to populate <select> options, how do make the select box show current option instead of the highest value?

StackOverflow https://stackoverflow.com/questions/23091348

  •  04-07-2023
  •  | 
  •  

Question

I have a select box that is auto-populated with options from a session variable. When you select an option and hit the apply button, the data relevant to that option is displayed in text boxes on the same page as the select (after a short process page). That part works fine.

They're sorted by value, so the highest option is always the "selected" one. If I happen to select an option other than the top one, the correct data will show, but the select box will still indicate the highest value is active.

Sorry, I don't explain things very well, I hope you could understand what I meant.

details: web based database management application, html/php/mysql/odbc

Was it helpful?

Solution

if I got it right, you want to maintain the option after form submission it depends on which method you use POST or GET if post you should use $_POST and if get you should use $_GET

you can use javascript to deal with it: this one will submit the form when you select the option, so no need for any button

in your html:

  <select id="sortSelect" class="sortSelect" size="1" name="sort" onchange="this.form.submit();">

            <option value="HighestValue" selected>Highest value</option>
            <option value="value1">value1</option>
            <option value="value2">value2</option>
        </select>

in javascript tags:

<script type="text/javascript">
    document.getElementById('sortSelect').value = "<?php if(! $_GET['sort']) echo $_SESSION[1]; else  echo $_GET['sort'];?>";
</script>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top