Aasim Azam
Aasim Azam

Reputation: 508

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.

Upvotes: 0

Views: 1723

Answers (4)

Ramu PL
Ramu PL

Reputation: 9

The shorter way is

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

Upvotes: 0

Aamir Afridi
Aamir Afridi

Reputation: 6411

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.

Upvotes: 0

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.

Upvotes: 0

Dev
Dev

Reputation: 479

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

Upvotes: 2

Related Questions