Page 1 of 1
Session variable - Basic question
Posted: Mon Mar 31, 2003 5:52 am
by jarow
I am very new to PHP, session variables and arrays so bear with me.
I have a 2 page form with several form fields on each page. I need to store the data from page 1 form fields, bring it along to page 2 and then submit all the data into my database.
My question is....is there a way of creating an array that would store all my data from page 1 or do I have to list each field separately? Would I need to create hidden fields on page two for the fields I have stored from page 1?
If you could please write out a basic example I would be most appreciative....and assume I know nothing!
<?php
session_start();
????
?>
Many thanks
Posted: Mon Mar 31, 2003 6:48 am
by volka
this example by far does not cover all aspects of sessions but it might be an entry-point

(your php version is 4.3.0, right? comments refer to default-settings)
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE); // debug-settings
/**
php sessions use cookies by default and cookies are sent within the response header
so this have to take place before any content (the response body, in this case an html-document) is written
*/
session_start();
/**
php has determined the session-id from the data sent with this request
and (if present) loaded the appropriate dataset from a flat file to the superglobal array $_SESSION
or created a new session-id
*/
?>
<html>
<body>
<?php
if(isset($_POST['mode']) && $_POST['mode']=='clear session')
{
// erase all elements of $_SESSION by assigning a new, emtpy array
$_SESSION = array();
echo '$_SESSION set empty';
}
elseif(isset($_POST['mode']) && $_POST['mode']=='destroy session')
{
/**
this does not! advise the client not to use the session-id anymore
but it turns off php's session handling, e.g. no serialization of session data will take place
*/
session_destroy();
echo 'session destroyed';
}
else
{
?>
your session id is: <?php echo session_id(); ?>
<fieldset>
old session values:
<pre><?php print_r($_SESSION); ?></pre>
<?php
/**
adding all parameters that have been POSTed with this request
by merging the arrays $_SESSION and $_POST
elements in $_POST having the same name as in $_SESSION will overwrite 'old' elements
*/
$_SESSION = array_merge($_SESSION, $_POST);
// and a simple counter
$_SESSION['counter'] = isset($_SESSION['counter']) ? $_SESSION['counter']+1 : 0;
?>
new session values:
<pre><?php print_r($_SESSION); ?></pre>
</fieldset>
<?php
} // end-of $_POST['mode']-cases
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
a field that always has the same name: <input type="text" name="static" /><br />
a field with a random name <input type="text" name="random<?php echo rand(1,1000); ?>" /><br />
you will figure out the name of this field <input type="text" name="field<?php echo count($_SESSION); ?>" /><br />
<input type="submit" name="mode" value="add" /><br />
<input type="submit" name="mode" value="clear session" /><br />
<input type="submit" name="mode" value="destroy session" />
</form>
</body>
</html>
<?php
/**
at the end of the script php will serialize $_SESSION if there's still a session active
*/
?>
before trying it run
check wether
session.save_path is set to a valid path.
Posted: Mon Mar 31, 2003 6:53 am
by d1223m
the simplest way is to use hidden fields in your pages but sessions work too.
for sessions look at session_register iirc. you simply pass to it the name of the variable you want to session'ize.
with hidden fields you simply do something like:
Code: Select all
---form1.php--
<form action="form2.php" method="post">
<input type="text" name="fred">
<input type="submit" >
</form>
--form2.php--
<form action="final.php" method="post">
<input type="hidden" name="fred" value="<? print $_POSTї'fred']; ?>">
<input type="text" name="bob">
<input type="submit">
</form>
--final.php--
print $_POSTї'fred'];
print $_POSTї'bob'];