Solved

Need to Remember Selection in SELECT field using PHP

Posted on 2009-04-08
4
179 Views
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>
</select>

Open in new window

0
Comment
Question by:archangelman
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
Phatzer earned 500 total points
ID: 24101654
Sure:

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

Then:

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

0
 
LVL 10

Expert Comment

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

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

:)
0
 

Author Closing Comment

by:archangelman
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.)
0
 

Expert Comment

by:CHRIS-WK
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.

Error:

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






<?php session_start();?>
<?php
$_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>
</form>

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

828 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