简体   繁体   中英

How to keep showing selected option from drop down list?

I have a drop down list where I select options

<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>

You can see the list here footystat

I am using the following PHP

if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; } 

if(isset($_POST['year'])){ $yearette = $_POST['year']; }

if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }

if(isset($_POST['which'])){ $whichette = $_POST['which']; }

When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.

Get Option value selected when it gets posted value, like this,

<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>

Set value like this for each option

You can set the "selected" property to the option , just like you set a value !

<option value="8" selected>2009/2010</option>

Use a if statement in PHP to determine which one should be selected.

Thats because the page refreshes.

On page load check if there is post variable than match the value with each option's HTML and write selected attribute.

更短的方法是

<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

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