You should spend some time familiarizing yourself with the layout of the online php manual. I spent about my first six months with php getting familiar with this document and it has been a god send ever since. Here I'll show the code to take my advice and point into the manual at the same time, hopefully killing two birds with one stone.
[assumptions]
myColor is the name of the seesion variable holding the person's current choice of color
ActionBtn is the name of the submit button on the html form.
FavColor is the name of the drop list and it's value is the color
[/assumptions]
our first step is to see if the person is using our form to select a new color
if($_POST['ActionBtn'] == 'Change Color') {
//code to process form based color change
}
Our next step is to see if the session variable is registered and ensure that the form takes precedence over this.
The first step check to see if the session variable is registered. I go to http://www.php.net and searched for session which took me here http://us4.php.net/manual/en/ref.session.php where I found this link http://us4.php.net/manual/en/function.session-is-registered.php to a function that
allows us to check if a certain session variable is set. This being the case we start our script with the following:
if($_POST['ActionBtn'] == 'Change Color') {
//code to process form based color change
} elseif(session_is_registered('myColor')) {
//code to process the session information into a local variable
}
And just to be safe we need to account for someone's very first visit with no cookie and not color value. So Finally we have the basic structure of out program which is this.
if($_POST['ActionBtn'] == 'Change Color') {
//code to process form based color change
} elseif(session_is_registered('myColor')) {
//code to process the session information into a local variable
} else {
//code for user who doesn't care about colors
}
Now we have to fill in each portion first I'll start with the default one, cause it just makes sense.
if($_POST['ActionBtn'] == 'Change Color') {
//code to process form based color change
} elseif(session_is_registered('myColor')) {
//code to process the session information into a local variable
} else {
$color = 'white';
}
Next I'll dor the form because I've got to get it writting the session before I can read the session.
if($_POST['ActionBtn'] == 'Change Color') {
if(!isset[$_POST['FavColor'] || $_POST['FavColor'] == "") {
$color = 'white';
$error = "Please select a color from the menu.<br />\n";
} else {
//start the session
session_start(); //http://us4.php.net/manual/en/function.session-start.php
//register my variable
$_SESSION['myColor'] = $_POST['FavColor']; //http://us4.php.net/manual/en/ref.session.php
//close the session and write the variables out
session_write_close(); //http://us4.php.net/manual/en/function.session-write-close.php
//set the local variable for use later in this script
$color = $_POST['FavColor'];
}
//code to process form based color change
} elseif(session_is_registered('myColor')) {
//code to process the session information into a local variable
} else {
$color = 'white';
}
Okay now we have the default and we have the session variable creation it's time to retrieve a session variable that's already set.
if($_POST['ActionBtn'] == 'Change Color') {
if(!isset[$_POST['FavColor'] || $_POST['FavColor'] == "") {
$color = 'white';
$error = "Please select a color from the menu.<br />\n";
} else {
//start the session
session_start(); //http://us4.php.net/manual/en/function.session-start.php
//register my variable
$_SESSION['myColor'] = $_POST['FavColor']; //http://us4.php.net/manual/en/ref.session.php
//close the session and write the variables out
session_write_close(); //http://us4.php.net/manual/en/function.session-write-close.php
//set the local variable for use later in this script
$color = $_POST['FavColor'];
}
//code to process form based color change
} elseif(session_is_registered('myColor')) {
$color = $_SESSION['myColor'];
} else {
$color = 'white';
}
Okay that's pretty much it. Now you just use $color and it will be this user's prefered color with a default of white and saving to a cookie, is that more helpful?