Page 1 of 1

$_SESSION and $_GET

Posted: Sun Oct 05, 2008 9:16 am
by kristolklp
I cannot understand why this is not working and I was hoping someone could explain it to me.

I have a form with 3 dropdowns, $year, $make, and $model. When a user selects a value out of one of the dropdowns I want to save it to a session variable. I am using onChange event on the form to reload the page and pass the value using $_GET (I am intentionally only passing 1 value at a time instead of all 3). When the page reloads, I want to save the passed variable to a $_SESSION variable for later use. This seems like this should be simple to do but it is not saving the value.

Here is how I am passing the value selected from the 3 dropdowns on the form (stripped down obviously):

Code: Select all

<select style="width: 90px;" tabindex="1" name="year" onchange="window.location= 'products.php?year='+this.value">
<select style="width: 90px;" tabindex="1" name="make" onchange="window.location= 'products.php?make='+this.value">
<select style="width: 90px;" tabindex="1" name="model" onchange="window.location= 'products.php?model='+this.value">
Here is how I am trying to save these value in the session:

Code: Select all

if ($_GET){
    if ($_GET['year']) $_SESSION['year'] = $_GET['year'];
    if ($_GET['make']) $_SESSION['make'] = $_GET['make'];
    if ($_GET['model']) $_SESSION['model'] = $_GET['model'];
}
Why does this not work when only passing 1 at a time? Do I have to pass all 3 with each call?

Re: $_SESSION and $_GET

Posted: Sun Oct 05, 2008 9:59 am
by Ziq
You should use session_start() function before using $_SESSION.

Re: $_SESSION and $_GET

Posted: Sun Oct 05, 2008 11:55 am
by kristolklp
Thanks for the reply. I am using session_start(); at the beginning of my script.

Re: $_SESSION and $_GET

Posted: Sun Oct 05, 2008 12:17 pm
by Ziq
Post all your code... generally it must work...

This is mine code. Compare this code and your one, maybe you find some errors yourself.

Code: Select all

 
<?
session_start();
if ($_GET){
    if ($_GET['year']) $_SESSION['year'] = $_GET['year'];
    if ($_GET['make']) $_SESSION['make'] = $_GET['make'];
    if ($_GET['model']) $_SESSION['model'] = $_GET['model'];
    
    print_r($_SESSION);
}
?>
<form method="GET">
<select style="width: 90px;" tabindex="1" name="year" onchange="window.location= 'index.php?year='+this.value">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select style="width: 90px;" tabindex="1" name="make" onchange="window.location= 'index.php?make='+this.value">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select style="width: 90px;" tabindex="1" name="model" onchange="window.location= 'index.php?model='+this.value">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</form>
 
 

Re: $_SESSION and $_GET

Posted: Sun Oct 05, 2008 12:49 pm
by kristolklp
Thanks for the response. I tested your code and it works as expected. I am communicating between flash (not my code) and PHP and it seems flash may be sending back an empty string that is overwriting the variable. My apologies, I thought the error had to do with the PHP handling of sessions.