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
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'];	

Link to comment
Share on other sites

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'];

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.