View Full Version : Learning Sessions

11-01-2009, 02:19 AM
Now that I've finally figured out what I need to be learning it is sessions.

(explaining the purpose of the code)

The form is real simple. The customer will only put in the amount they want.

<form action="order.php" method="post" style="height: 460px">
order01<input name="order01" style="width: 40px" type="text" /> <br />
order02<input name="order02" style="width: 40px" type="text" /><br />
order03<input name="order03" style="width: 40px" type="text" /><br />
<br />
<input type="submit" value="Submit"onclick="return confirm('Are you sure you want to submit the form?');" />
<input name="reset" id="reset" type="reset" value="Reset" />

The php will only print what is ordered and leave out any '0's it encounters.
Now what I don't know how to do is get information from this php (Confirmation Page) to the email. All I want is only the information that is printed on the confirmation page. I've tried to read up on sessions but i don't comprehend it that well. If anyone can show me an example based upon this code I can learn it. Thanks to all that have any comments or critiques.

$order01 = $_POST["order01"];
$order02 = $_POST["order02"];
$order03 = $_POST["order03"];

if ( $order01 > 0 ) {
echo " amount of $order01 <br />";

if ( $order02 > 0 ) {
echo " amount of $order02 <br />";

if ( $order03 > 0 ) {
echo " amount of $order03 <br />";
<form method="post" action="">
<input type=button value="Back" onClick="history.go(-1)">
<input name="Submit" type="submit" value="Place Order" onClick="testResults(this.form)">

11-01-2009, 09:21 PM
Post values to email;

// Subject.
$subject = 'Results from Contact form';
// Email address.
$emailadd = 'mail@example.com';

// Redirect after the form is processed.

//Do not edit below this line
$text = "Results from form:\n\n";
$space = ' ';
$line = '';
foreach ($_POST as $key => $value){
for ($i = 1; $i++){
$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

11-01-2009, 09:50 PM
regarding sessions, in a nutshell

session_start(); // must be at the TOP of EVERY page you want to use sessions on.

/* in its simplest form. here we set some session variables */

$_SESSION['username'] = 'met';
$_SESSION['age'] = 21;

echo 'My username is ' . $_SESSION['met'] . ' and I am ' . $_SESSION['age'];
// outputs My username is met and I am 21

unset($_SESSION['username']); // unsets the value

echo $_SESSION ['username']);
// outputs nothing.

/* destroys all sessions and all data is lost */

sessions are used to store data and transfer it across multiple pages (as well as other functions)

some examples: storing the contents of a shopping cart, authenticated user details, page viewing history, user preferences...

you can set a session by assigning it a value literally, or assigning it from $_GET or $_POST (using forms eg)

in your example above when the user submits the form you could set a session value to be equal to the amount they entered, and then use that information on another page later on.

$_SESSION[] is an array, much like $_GET and $_POST, you set and access the values the same way. The main difference is that SESSION will last for the duration of, well, the users session, whereas GET and POST have less scope, i.e only the form(s) they are used in.

the most important thing when dealing with sessions is to remember to put


/* your script */

at the very top of every page youwant to use sessions in.