FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Storing A Lot of Values Using Sessions





TheGremlyn
Recap of what I'm doing for this school applied project:

The police have this paper form that's on a legal size piece of paper. It's long and it looks even longer when we put it in HTML format with PHP posting to our database. I want to break up the form into smaller sections so if they make a mistake they don't have to go all the way to the top to make sure a radio button is set (we are also having trouble getting radio buttons and check boxes to maintain their selected state if the user misses a field. The text boxes and text areas are fine.). I was hoping that I could use sessions to store the information and carry it over as they filled in the form until the final submission that posts to the database in one go.

I'm using sessions to log users in and allow access to aspects of the site. My concern is that if I use more sessions to store these values and then I go to clear or stop them then I will also inadvertently log the user out as well after they submit the booking form data.

Code:
<?php
session_start();

if($_SESSION['court']!=1)
{
echo
'<div class="box">
<label>
</br><h2>You must be logged in to view this content.</h2></br>
</br><a href="login.php"><h3>Return to login page:</h3></a></br>
</label>
</div>';
return;
}
?>


This section of code above is an example of how I ensure authorized access to different aspects of the site. If the user is logged in and they log out i use this code below from another file that is called when they hit "Sign Out"

Code:
session_start();
session_destroy();


With this in mind, if I use this kind of code:

Code:
$_SESSION['fname'] = $_POST['fname'];  // Prisoners first name
$_SESSION['lname'] = $_POST['lname];  // Prisoner last name
$_SESSION['city'] = $_POST['city'];  // City the prisoner is from
$_SESSION['yo_status'] = $_POST['yo_status'];  // Prisoner's young offender status (usually yes or no)


and if I store almost 80 different variables worth of information (can be simple 'yes' 'no' answers or longer comments about the booking situation), will this work and what happens at the end when they submit this information at the end to the database? I'd have to clear this session information since I won't need it after its in the database until much much later. But how would I do that without ending the login session information?

I don't want sections of the form to be submitted to the database as they go through since the officer may cancel out and I'll have a half filled row in the system unless I can delete the information. I want the user to go through every aspect of the form in sections, then by the end they can cancel and just chuck the session data or they can submit and post to the database and then clear the sessions of the information they were storing (except the login information).

I'm kind of new to this and I'm very careful with how I do this kind of code, but I really can't find an example that includes these different aspects.

Hopefully I've explained this well enough and if this is the way to go then I'll just need a little clarification. If this is completely wrong and it would waste a ton of resources or there's a different way of doing things then I want to know. I want to get this right and I want the sponsors to be happy with the system so they can say goodbye to the old and hello to the new.
jmraker
You can use sessions to store the form info. Since each $_SESSION is written to a single file (one file for every session id) the only limits are filesystem based like free disk space. Instead of using

Code:
$_SESSION['fname'] = $_POST['fname'];  // Prisoners first name
...


I would use

Code:
$_SESSION['form1']['fname'] = $_POST['fname'];  // Prisoners first name
$_SESSION['form1']['fname'] = $_POST['fname'];  // Prisoners first name
$_SESSION['form1']['lname'] = $_POST['lname];  // Prisoner last name
$_SESSION['form1']['city'] = $_POST['city'];  // City the prisoner is from
$_SESSION['form1']['yo_status'] = $_POST['yo_status'];  // Prisoner's young offender status (usually yes or no)


That way every field would be in $_SESSION['form1'] and you can do things like loop through the $_SESSION['form1'] array for all the fields, remove them all with unset($_SESSION['form1']), plus if there's another form with that name they don't overwrite each other.

A side effect of this is that without a better way to organize the session the same user couldn't fill out 2 or more forms at once, as one would overwrite the other in the session and it could save to the database with parts of the parts filled in another window/tab


Another and perhaps better way to go is to put the whole form on one page, break it into steps and make it like a wizard where you only see 1 step at a time with navigation to switch steps and a next button. It would use javascript and do something like

http://www.wbotelhos.com/stepy/
http://www.jankoatwarpspeed.com/post/2009/09/28/webform-wizard-jquery.aspx
http://thecodemine.org/
TheGremlyn
I really like what you can do with jQuery, but it frightens me right now. I will definitely bookmark those links and read up on them and see if I can apply them to something on a smaller scale. Trying to apply something like that to a very large form would probably be a nightmare and a half.

I think I understand the sessions aspect a little bit more and being able to clear a session by name will help ease my mind. I will probably give that a shot soon and see if I can get it to work on a few smaller sections to test how it goes in to the database.

Thanks a lot of your input, I really appreciate it!
badai
think of session just a way to pass information from one page to the other.

instead of using

$_SESSION['fname'] = $_POST['fname'];
$_SESSION['lname'] = $_POST['lname'];
$_SESSION['city'] = $_POST['city'];
$_SESSION['yo_status'] = $_POST['yo_status'];

it will be better if you use object:

$person->fname = $_POST['fname'];
$person->lname = $_POST['lname'];
$person->city = $_POST['city'];
$person->yo_status = $_POST['yo_status'];

then you can have only 1 session:
$_SESSION['person'] = serialize($person);

at the other page, re-create $person using:
$person = unserialize($_SESSION['person']);

you can also use array instead of object. with proper data structure, it will be easier to keep track, manage and manipulate your data like using loop.

since you have a lot of stuffs in your form, do as jmraker suggested: use a wizard so that user can put information bit by bit and go back and forth in case they change their mind or made a mistake.

to avoid user opening a form in two different windows/tab, you can check first if that session exist (in the first page that should create the session), and for subsequent pages (in case you use wizard), just create a unique session for each page. just don't forget to unset it when you exit the page (don't unset the session that you want to pass along), and unset session that you no longer use.
sonam
There is lot of ways to using sessions in PHP but for me is always important to using simplest way. In your situation I will follow your logic with few question on one page (without scrolling) and if some field empty return on previous. Even more you can do it all on one page with sessions and only last two-three fields check in one outside php and store in database. This is short example and I know lot of programmers don't like @ in theirs scripts.


Code:
<?php
session_start();
error_reporting(E_ALL);
ini_set("display_errors", "1");
ini_set('error_reporting', E_ALL);

// this part fill sessions
if(@$_POST['send'] == 'Send') {
   $_SESSION['name'] = trim(@$_POST['name']);
   $_SESSION['surname'] = trim(@$_POST['surname']);
}

// this part check second input and fill sessions NOTE: the field name for "SEND" is different then first one
if(@$_POST['send2'] == 'Send') {
   $_SESSION['birth_day'] = trim(@$_POST['day']);
   $_SESSION['birth_month'] = trim(@$_POST['month']);
   $_SESSION['birth_year'] = trim(@$_POST['year']);
}

// this part check first part of form
if(empty($_SESSION['name']) || empty($_SESSION['surname'])) {
   $form = '<form name="kontakt" method="post" action="'. $_SERVER['PHP_SELF'] . '">   
     <span class="bold">Name:</span><br /><input type="text" name="name" value="' . @$_SESSION['name'] . '" /><br />
     <span class="bold">Surname:</span><br /><input type="text" name="surname" value="' . @$_SESSION['surname'] . '" /><br />
     <input type="submit" name="send" value="Send" />
    </form>';
// this part check second part of form   
} elseif(empty($_SESSION['birth_day'])|| empty($_SESSION['birth_month']) || empty($_SESSION['birth_year'])) {
   $form = '<form name="kontakt" method="post" action="'. $_SERVER['PHP_SELF'] . '">   
     <span class="bold">Birth day:</span><br /><input type="text" name="day" value="' . @$_SESSION['birth_day'] . '" /><br />
     <span class="bold">Birth month:</span><br /><input type="text" name="month" value="' . @$_SESSION['birth_month'] . '" /><br />
     <span class="bold">Birth year:</span><br /><input type="text" name="year" value="' . @$_SESSION['birth_year'] . '" /><br />
     <input type="submit" name="send2" value="Send" />
    </form>';
} else {
   // you can use here lot of other elseif before last one
   echo ':)';
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="hr" lang="hr">
<head>
    <title>Form example</title>
</head>
<body>
<?php echo $form; ?>
</body></html>



Sonam
Related topics
php sessions
Do you know a free tool like codecharge studio?
How To : Secure Your PHP Website
Sessions
CSS/Layout Help Please!
FireFox and Cookies/Sessions
$_COOKIE and $_SESSION help needed!
Cookies not getting set
I need some help please!
PHP using Sessions
Addition to Bonding's Backup Thread
Sessions & cookies - member no longer active question
maybe i'm daft...
Hello to all!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.