Need to Remember Selection in SELECT field using PHP

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

Who is Participating?
PhatzerConnect With a Mentor Commented:

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

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

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

archangelmanAuthor Commented:
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.)
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.