简体   繁体   中英

Making selected option default option of dropdown when move to another page

I have created a mobile menu on my site with the html below:

 <select id="mobile-select" name="mobile-select" onchange="if (this.value) window.location.href=this.value"> <option class="mobile-select-label" value>Navigation</option> <option class="mobile-select-label" value="http://www.themathsproject.co.uk/home">Home</option> <option class="mobile-select-label" value="http://www.themathsproject.co.uk/projects/all">Projects</option> <option class="mobile-select-label" value="http://www.themathsproject.co.uk/tutoring">Tutoring</option> <option class="mobile-select-label" value="http://www.themathsproject.co.uk/contact">Contact</option> </select> 

When the selects an option on the dropdown, they're sent to the appropriate page. When on that new page, I want the default option (shown on the dropdown) to correspond to the new page.

Eg if the user had clicked Tutoring, the page would reload to the Tutoring page. Once on the Tutoring page, I want the dropdown to show "Tutoring" by default.

Note how if the user clicks "Projects", they're sent to "../projects/all" instead of just "/projects".

I have tried to implement several other techniques (found on very similar stack exchange questions) but couldn't get any to work.

Note: The onchange attribute is responsible for directing the user to the appropriate page after a selection has been made.

Any help would be greatly appreciated,

Jack

1)you can use query string
2)or you can also do it by browser local storage
3)or cookie

You can use the following code

$current_url  = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
<option class="mobile-select-label" <?php echo ($current_url == 'http://www.themathsproject.co.uk/tutoring' ? 'selected' : '') ?> value="http://www.themathsproject.co.uk/tutoring">Tutoring</option>

same as do for others link

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