Jump to content
Larry Ullman's Book Forums

Multi-Page Forms With Sessions


Recommended Posts

Larry,

I asked about multi page forms a few months ago, you explained it a bit, but I still can't figure it out. I understand that you have to use a combination of $_SESSION and $_POST variables to jump from page to page and then insert the data into the database... however...

 

If the first form page has say, 3 text inputs (username, password, and confirm password) and I want the second page to have one text field for their email address, I don't understand how to accomplish this if the first page displays and processes the form. The form action is "register.php" and it processes and displays the form. If I set the form action to "register2.php", the page that contains the other input I want filled out, the items on the first page never get validated. Yes, 3 text inputs on one page and 1 on the other is not a good use of a multi page form, but I wanted to keep it simple until I figure it out.

 

After the validation routines on the username and password fields, each gets assigned to the variables $username and $password. Also, I assign $_SESSION['username'] and $_SESSION['password'] the values of $username and $password, respectively.

 

But how do I get to the next page (register2.php)? I know I can use the session variables on that next page, and when THAT page is completed, I can send the $_SESSION variables from page 1 and the $_POST variable from page 2 to the database.

 

I just don't see how to validate page 1, then go to page 2 if the form can only have one action.

 

Any help is appreciated.

 

-Lou

Link to comment
Share on other sites

Larry, I think I figured it out. I feel so stupid... just use a header redirect to 'register2.php' once everything is validated on register.php, right? Then send the data to the database... the $_SESSION variables from page 1, mixed with the $_POST variable from page 2. You must have said it a bunch of times in your books... after awhile you see the same techniques used to accomplish other tasks.

 

This is what I'm talking about... deep inside my brain I should know what to do, but I just can't put 2+2 together. I was so busy trying to think how to get to the 2nd form page with the form action, that I completely forgot about my knowledge of headers and how you can redirect, change the content type (to say, send text instead of html), etc.

Link to comment
Share on other sites

Another quick question...

 

How do I block users from accessing 'register2.php' without first completing 'register.php'? I tried checking if the $_SESSION variables are set (at the time $_POST is validated I also assign it to $_SESSION['something'] so I can pass it to the next page via a location header), but if the user sets them once (say they fill out register,php, then get to register2.php, then decide they don't want to sign up) so that doesn't work. I also tried using hidden inputs until i realized hidden inputs only work on the action page, and i'm using headers to redirect.

Link to comment
Share on other sites

A common technique used is to create an error array. This can later be used BOTH to check if the user can PROCEED to the next page AND to DISPLAY errors.

 

This is how you do it:

 

$errors = array(); // Create the array in the computers memory

if (!isset($_POST[something]) or empty($_POST[something]) 
{
  $errors[] = "Error message";
}
else 
{
  // All is good. Data is validated.
}

if ( $errors == null ) // There is no breakage of the validation rule. Data and LIFE is good!
{
  // The errors array has NO errors. Therefor we can redirect the user with header. 
}
else // This will display all errors from the bad data
{
  foreach ( $errors as $msg ) {
 	echo $msg; 
  }
}

Link to comment
Share on other sites

@Hartley...

 

How would I have a flag variable though? I'm redirecting to register2.php with a header. So I can't use a hidden input on register.php, since that would only be available to the processing script, which would be register.php. If I store the flag variable in a session, it will always be available after the first time a user fills out register.php successfully. If they later decide not to finish the 2nd part, and go to different pages on the site, they could go directly to register2.php without first refilling out the first form.

 

I have to think about this.

Link to comment
Share on other sites

Lou, you're right. I didn't do a very good job of explaining myself.

 

You will infact want to set a session cookie with a short time limit, and check for the existence of that session cookie on the second form. Hope that helps.

Link to comment
Share on other sites

But isn't a cookie sent automatically when you start a session, with the same name as the session? I can over ride this cookie and add a time limit? But won't that make all of my $_SESSION variables have that time limit? I only want the $_SESSION variables associated with page 1 of the form to have that short time limit.

 

Confused... read three books on sessions and only one so far has even talked about time limits on sessions... PHP Solutions by David Powers.

Link to comment
Share on other sites

I could destroy the session and cookie after the registration is done. This is just a sample registration form that i'm using to learn about multipage forms. The first form is 3 text inputs, you proceed to the next form, which is just one input. Then the three session variables and one post variable get sent to the database. After that point, I destroy the session variables. It's just that register2 page that I don't want people to be able to access for very long after they fill in the first page. I'm close to figuring this out.

Link to comment
Share on other sites

Yes you can create your own cookie instead of letting the session create one and give it a short time limit. I read about this in the "PHP and MySQL Web Development" book by Luke Welling. A book pretty much useless except for a few nuggets of info. Larry writes the cleanest code of any author I've read.

Link to comment
Share on other sites

Thanks for the nice words, Lou!

 

Well I do read other books but I always come back to yours for clarification on some issues. Some books cover more material but use a lot of user made functions that have the reader bouncing back and forth to figure out what does what. Sometimes it's too convoluted to make sense of, sadly.

Link to comment
Share on other sites

You could of course set the flag AFTER all the $_POST-variables are right

 

 

$flag = null;

isset($_POST['first'], $_POST['second'], $_POST['third'], $_POST['fourth'], $_POST['fifth'], $_POST['sixth']) {
  echo 'This is actually allowed! All must be set! USE IT';
  $flag = TRUE;
}

if ($flag == TRUE) {
  header(); // send them to next page
}

Link to comment
Share on other sites

 Share

×
×
  • Create New...