Solved

Need to Remember Selection in SELECT field using PHP

Posted on 2009-04-08
4
181 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

687 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