tags:

views:

33

answers:

1
$Var = new StdClass;

if($_POST['somvar']){
$Var->somvar = $_POST['somvar']
}
else
{
 $somevar=''
}

Why is it creating hidden varaible for this statement "$Var->somvar = $_POST['somvar']" when i see the view source

How do i persist the state of this variable when moving to next pages

A: 

The answer to your second question is probably sessions.

session_start();

$Var = new StdClass;

if($_POST['somvar']){
$Var->somvar = $_POST['somvar']
}

// Objects need to be serialized to be stored in $_SESSION
$_SESSION["Var"] = serialize ($Var);

to access $Var on another page:

session_start();

if (array_key_exists("Var", $_SESSION))
 $Var = unserialize($_SESSION["Var"]);

if (!empty($Var->somvar))
 echo "Somvar is: ".$Var->somvar;   
Pekka
"I don't understand" stuff belongs to the "comments" section.
stereofrog
@stereofrog true. I felt it okay in this case, seeing as it's a combination of questions. Still, editing.
Pekka
You don't necessarily have to serialize objects to put them into sessions. It's just a matter of making the definitions available upon session_start or use an autoloader.
Artefacto