Just wondering how I could save all the data from a form to a session variable as soon as a user navigates to another link. When they come back all of the form is field out to where they left off.
Is this posible?
I know cookies is another way but would rather stick to php, it also allows me to lean how to use sessions.
TIA,
Wes/
variables and sessions
Moderator: General Moderators
It's important to htmlspecialchars() any post vars which might get sent back to the form for security reasons and to take care of any quotes.
Setting session vars would go something like this:
To display previous entries in the form, you first need to check $_SESSION:
Come to think of it, that should probably be a little fn:
Finally, set the value attribute of form input fields to $var1, $var2 etc. If no corresponding session var was found, $vars have been set to null hence avoiding any "undefined index" errors.
General info on sessions:
viewtopic.php?t=6521
Setting session vars would go something like this:
Code: Select all
<?php
$_SESSION['var'] = htmlspecialchars($_POST['var']);
?>Code: Select all
<?php
if(isset($_SESSION['var']))
{
$var = $_SESSION['var'];
} else {
$var = null;
}
?>Code: Select all
<?php
function getSessionVar($name)
{
if(isset($_SESSION[$name]))
{
return $_SESSION[$name];
} else {
return null;
}
}
// in use
$var = getSessionVar('var');
?>General info on sessions:
viewtopic.php?t=6521
Last edited by McGruff on Wed Aug 10, 2005 3:50 pm, edited 1 time in total.