Solved

Need to Remember Selection in SELECT field using PHP

Posted on 2009-04-08
4
176 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
Comment Utility
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
Comment Utility
Sorry, I noticed on that form you weren't posting, but getting:

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

:)
0
 

Author Closing Comment

by:archangelman
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
php mail headers 2 8
MySQL database data submission 7 38
php image upload 3 24
Page showing diff display 4 19
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
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…
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 …

744 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

17 Experts available now in Live!

Get 1:1 Help Now