Need to Remember Selection in SELECT field using PHP

Posted on 2009-04-08
Last Modified: 2012-05-06
Assume we have a list of States in a form.  Using PHP, how can the selected state be retained if the user submits the form, then for example clicks back in their browser.  (I know that the page woudl not render, but it is simply given as an example so I can explain what I need.)

So, say that California is selected, I need that selection to be maintained in the event the form is viewed again in the same session.  Is there a way to do this easily in PHP, say with a session variable or something?
Assume the following form.  If this is submitted, how can the value selected be retained if the user clicks back?

<form action="./" method="GET">

<select name="state">

	<option value="AL">Alabama</option>

	<option value="AK">Alaska</option>

	<option value="AZ">Arizona</option>

	<option value="AR">Arkansas</option>

	<option value="CA">California</option>

	<option value="CO">Colorado</option>

	<option value="CT">Connecticut</option>

	<option value="DE">Delaware</option>

	<option value="DC">District of Columbia</option>

	<option value="FL">Florida</option>

	<option value="GA">Georgia</option>

	<option value="HI">Hawaii</option>

	<option value="ID">Idaho</option>

	<option value="IL">Illinois</option>

	<option value="IN">Indiana</option>

	<option value="IA">Iowa</option>

	<option value="KS">Kansas</option>

	<option value="KY">Kentucky</option>

	<option value="LA">Louisiana</option>

	<option value="ME">Maine</option>

	<option value="MD">Maryland</option>

	<option value="MA">Massachusetts</option>

	<option value="MI">Michigan</option>

	<option value="MN">Minnesota</option>

	<option value="MS">Mississippi</option>

	<option value="MO">Missouri</option>

	<option value="MT">Montana</option>

	<option value="NE">Nebraska</option>

	<option value="NV">Nevada</option>

	<option value="NH">New Hampshire</option>

	<option value="NJ">New Jersey</option>

	<option value="NM">New Mexico</option>

	<option value="NY">New York</option>

	<option value="NC">North Carolina</option>

	<option value="ND">North Dakota</option>

	<option value="OH">Ohio</option>

	<option value="OK">Oklahoma</option>

	<option value="OR">Oregon</option>

	<option value="PA">Pennsylvania</option>

	<option value="RI">Rhode Island</option>

	<option value="SC">South Carolina</option>

	<option value="SD">South Dakota</option>

	<option value="TN">Tennessee</option>

	<option value="TX">Texas</option>

	<option value="UT">Utah</option>

	<option value="VT">Vermont</option>

	<option value="VA">Virginia</option>

	<option value="WA">Washington</option>

	<option value="WV">West Virginia</option>

	<option value="WI">Wisconsin</option>

	<option value="WY">Wyoming</option>


Open in new window

Question by:archangelman
  • 2
LVL 10

Accepted Solution

Phatzer earned 500 total points
ID: 24101654

make sure session_start() is at the right at the top of each PHP page using the session variable first...


When posted to:

$_SESSION['state'] = $_POST['state'];

Then with the code you'd need to have it laid out as such, you can automate it if you're drawing the states form the database, otherwise its a bit more drawn out...
<select name="state">

        <option value="AL"<?php if(isset($_SESSION['state'] && $_SESSION['state']=="AL"){ print(" selected=\"selected\""); } ?>>Alabama</option>

        <option value="AK"<?php if(isset($_SESSION['state'] && $_SESSION['state']=="AK"){ print(" selected=\"selected\""); } ?>>Alaska</option>

and so on...

Open in new window

LVL 10

Expert Comment

ID: 24101772
Sorry, I noticed on that form you weren't posting, but getting:

$_SESSION['state'] = $_GET['state'];


Author Closing Comment

ID: 31568251
Thank you for the fast reply.  Tested and confirmed working.  (And I caught the Post / Get issue too.  Actually, the form uses POST now anyway.)

Expert Comment

ID: 33606844
Hi there!

I was trying to use the solution Phatzer posted but I'm getting some parse error. Maybe I didn't use the sessions correctly, I just want to test if the selected option is rememberd after the for is submitted and there are still errors on the form. A better example is let's say I have a STATE dropdown select option and a BIRTHDAY dropdown too, if the use selected the STATE but not the BIRTHDAY I want the selected STATE to be remembered.


Parse error: parse error, expecting `','' or `')'' in  M:\KIT\xampp\htdocs\print.php on line 8

<?php session_start();?>
$_SESSION['state'] = $_POST['state'];
<form method="post">

<select name="state">
<option value="AL"<?php if(isset($_SESSION['state'] && $_SESSION['state']=="AL"){ echo(" selected=\"selected\""); } ?>>Alabama</option>
<option value="AK"<?php if(isset($_SESSION['state'] && $_SESSION['state']=="AK"){ echo(" selected=\"selected\""); } ?>>Alaska</option>

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now