using session to store data and use at a later stage
Moderator: General Moderators
using session to store data and use at a later stage
Hi there,
I am building an online insurance booking form which over several screens will take details of people to be insured.
On the first screen, the client specifies the amount of people he wants to insure. On the second screen I have used a loop to create the correct amount of spaces to take the details of each person he wants to insure.
Now where i'm getting stuck (and this is probably something strait forward) is how I store each of these person's details in the session and then recall them a couple of screens later to firstly confirm the client's details but then also to save onto the server for the insurance to be issued.
Information such as telephone number and address for example is easy because I know that there will only be one variable name to store in the session, but with the list of client's details I am not going to know how many there as that is controlled by the person entering in the amount on the front page.
I have got as far as creating a loop which as well as creating the correct amount of spaces for the details but it also names each text box a unique name i.e. name1, age1, name2, age2, name3, age3 etc.
Do I add something to the loop which will add each of the details to a session variable eerytime it loops?
Any help or suggestions would be really appreciated.
Many thanks
will
I am building an online insurance booking form which over several screens will take details of people to be insured.
On the first screen, the client specifies the amount of people he wants to insure. On the second screen I have used a loop to create the correct amount of spaces to take the details of each person he wants to insure.
Now where i'm getting stuck (and this is probably something strait forward) is how I store each of these person's details in the session and then recall them a couple of screens later to firstly confirm the client's details but then also to save onto the server for the insurance to be issued.
Information such as telephone number and address for example is easy because I know that there will only be one variable name to store in the session, but with the list of client's details I am not going to know how many there as that is controlled by the person entering in the amount on the front page.
I have got as far as creating a loop which as well as creating the correct amount of spaces for the details but it also names each text box a unique name i.e. name1, age1, name2, age2, name3, age3 etc.
Do I add something to the loop which will add each of the details to a session variable eerytime it loops?
Any help or suggestions would be really appreciated.
Many thanks
will
Using session in PHP is easy 
ensure that you have:
on every page, and then anything stored in the session will be in the superglobal $_SESSION
as an example, page1.php:
page2.php:

ensure that you have:
Code: Select all
<?php
session_start();
//rest of page
?>as an example, page1.php:
Code: Select all
<?php
session_start();
$_SESSION['variable'] = 'Hello world!';
header('Location: http://www.somesite.com/page2.php');
exit();
?>Code: Select all
<?php
session_start();
echo $_SESSION['variable']; //outputs 'Hello world!'
?>Hi, yes,and thank you, I found that out last night.
but my problem lies how do I store my dynamically created client details in the session variables to then be able to extract at a later page.
i.e.
PAGE1: user wants to insure 3 people.
PAGE2: 3 rows of text box's are created for user to enter details.
PAGE3: just a page for inserting address etc.
PAGE4: All of the details are displayed, incl the three rows that the client entered on PAGE2.
You see the 3 rows are dynamically created and the amount of rows produced is specified by the amount the user enters on PAGE1.
Do you know what I can do
Thanks
but my problem lies how do I store my dynamically created client details in the session variables to then be able to extract at a later page.
i.e.
PAGE1: user wants to insure 3 people.
PAGE2: 3 rows of text box's are created for user to enter details.
PAGE3: just a page for inserting address etc.
PAGE4: All of the details are displayed, incl the three rows that the client entered on PAGE2.
You see the 3 rows are dynamically created and the amount of rows produced is specified by the amount the user enters on PAGE1.
Do you know what I can do
Thanks
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
Ok I see
But the number of lines are dependent on how many the ppl the user wants to insure.
So how could I dynamically change that number in the variable assignement according to the amount needed to be insured and more so how do I recall that correct amount when I display them on screen.
I think I need to go and read a tutorial book.
But the number of lines are dependent on how many the ppl the user wants to insure.
So how could I dynamically change that number in the variable assignement according to the amount needed to be insured and more so how do I recall that correct amount when I display them on screen.
I think I need to go and read a tutorial book.
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
Ok thanks for that -
So when I come to displaying the variableson screen I can create a count loop and do:
?
So when I come to displaying the variableson screen I can create a count loop and do:
Code: Select all
<?php
$pcount = 6;
$i = 0;
while ($i < $pcount) {
print "<tr>";
print "<td>";
print "$_SESSION['people'][$i]";
print "</td>";
print "</tr>";
$i++;
}
?>?
- n00b Saibot
- DevNet Resident
- Posts: 1452
- Joined: Fri Dec 24, 2004 2:59 am
- Location: Lucknow, UP, India
- Contact:
nah!
pay attention to the 7th line. all elements in the sub-array would not be printed automatically... you have to print them by explicitly coding the indices...
and for god's sake use PHP tags for posting code...
Code: Select all
<?php
$pcount = count($_SESSION['people']);
for($i = 0; $i < $pcount; $i++) {
print "<tr>";
print "<td>";
print $_SESSION['people'][$i]['whatever'];
print "</td>";
print "</tr>";
}
?>and for god's sake use PHP tags for posting code...
Code: Select all
<?php
foreach($_SESSION['people'] as $person) {
echo "<tr><td>{$person['whatever']}</td></tr>\n";
}
?>-
Charles256
- DevNet Resident
- Posts: 1375
- Joined: Fri Sep 16, 2005 9:06 pm