Jump to content

Session and default value


Bravat

Recommended Posts

I have two sessions on one page:

Session #1:

if(isset($_POST['sezonaSubmit'])){
							$_SESSION['sezona'] = $_POST['sezona'];
						} else {
							$_SESSION['sezona'] = "Letnja Guma";
							}
		$sezona = $_SESSION['sezona'];		

 

Session #2:

if(isset($_POST['poredak'])){

$_SESSION['poredak'] = $_POST['red'];

} else {

$_SESSION['poredak'] = " ORDER BY price ASC";

}

$order = $_SESSION['poredak'];

 

And they are used to change order on the list. Problem is when I change one, other session is changed to the default value. How to fix this?

Link to comment
https://forums.phpfreaks.com/topic/238686-session-and-default-value/
Share on other sites

I think you should wrap each in its own IF statement.

IF ($_POST['sezonaSubmit']){
if(isset($_POST['sezonaSubmit'])){
$_SESSION['sezona'] = $_POST['sezona'];
} else {
$_SESSION['sezona'] = "Letnja Guma";
}
}//end if $_POST['sezonaSubmit']
$sezona = $_SESSION['sezona'];

IF ($_POST['poredak']){
if(isset($_POST['poredak'])){
$_SESSION['poredak'] = $_POST['red'];
} else {
$_SESSION['poredak'] = " ORDER BY price ASC";
}
}//end if $_POST['poredak']
$order = $_SESSION['poredak'];	

You only have ONE session, but you are maintaining two session values.

 

I think the problem is that you have two different forms and based upon which one is submitted you are setting the value for one variable and the other is getting set to the default. You should check to see if the session value was previously set before setting it to the default. For each one I would

1. First check if something was posted (if so use the posted value)

2. Then I would check if the session value was previously set (if not then set to the default)

3. If the session value was already set, then I would do nothing and just use that value

 

if(isset($_POST['sezonaSubmit']))
{
    $_SESSION['sezona'] = $_POST['sezona'];
}
elseif(!isset($_SESSION['sezona']))
{
    $_SESSION['sezona'] = "Letnja Guma";
}                        }
$sezona = $_SESSION['sezona'];      


if(isset($_POST['poredak']))
{
    $_SESSION['poredak'] = $_POST['red'];
}
elseif(!isset($_SESSION['poredak']))
{
    $_SESSION['poredak'] = " ORDER BY price ASC";
}
$order = $_SESSION['poredak'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.