Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hiya,

I've not posted a question for a while so I hope you're all well.

 

My question is more of a general one as I can use sessions to solve the problem.

 

I have 3 forms on 3 pages. The idea being to ask the user for information and pass that information in the url to the next page and then pass the same information plus some more attained from the form onto the 3rd page. I know that I can use sessions for this however I would like to understand the $_GET array a bit more because I feel that I should be able to do this without sessions.

 

The problem I'm having is getting the information into the url for the subsequent pages. I can pass information from the first form and when it redirects the user to the second form the $_GET array contains the information. So far so good! The second form uses the $_POST array because it needs to validate the additional information. When the form is submitted the $_GET array 'empties'. I understand why that's the case.

 

So I tried the following in order to use the information in the $_GET array on that second page:

1. Assigning the $_GET to a variable. However when the form is submitted it tries to do it again and there's an error because there's nothing in the $_GET array.

2. Assigning the values in the $_GET array to a cookie. However the information is not available until the page reloads.

 

In a nutshell how do I get GET information back into the url so as to pass it onto other pages.

 

Cheers

Paul

 

Link to comment
Share on other sites

For starters, I wouldn't use the $_GET array.

The only time I would use the $_GET array for a form is for a search form in which you want the resulting query to be "bookmarkable".

 

For a standard registration form though, I wouldn't use the $_GET array, as it exposes all that data in the URL, which, if nothing else, is bad form. (After all, would you feel safe and secure if the password you just entered was openly displayed in the URL?)

 

With that said, I would instead post all three pages of the form, and then store the data from the previous page(s) in either the session (as you mentioned) or hidden input fields.

 

With all that said though, my personal favorite solution is to have the whole form on one page to begin with. While I acknowledge that there're times in which you need a multipage form, in general, I really don't like multipage forms.

 

And if you really do require a multipage form though, you could always use JS to power the whole thing, in which case, you wouldn't have to worry about losing data from "page" to "page".

Link to comment
Share on other sites

Hiya HartleySan,

Thanks for the response.

 

I'll use sessions I think.

 

The reason I'm using multiple pages is that some fields are only going to be displayed depending on the answers to previous questions. Yes, this can be done in JS but I'm creating a non-JS version first.

 

Cheers

Paul

Link to comment
Share on other sites

Hiya,

Really! I've tried that before and got myself in a right mess.

 

If you had form 1 then if {isset($_POST['submitted'] etc. would apply to form 1. Then presumably you would validate that, assign it to variables and then load form 2 based on the answers in form 1.

 

However how would you ensure that form 1 never reloaded? Would you need to nest forms within an if / else?

 

I've always used the same approach. Code at the top with an {isset($_POST['submitted'] and the single form below. I must be getting old because it's never ossurred to me that more than one form can be on the same page.

 

Is my thinking going in the right direction (the nesting within an if/else, not whether I'm getting old. I know that!)?

 

Cheers

Paul

Link to comment
Share on other sites

Having the whole form on one page, while it will slightly increase the PHP logic for that page, will ultimately be easier to handle than a multipage form.

 

The basic logic would probably be something like the following:

if (form-submitted) {
  
  // Validate initial inputs.
  
  if (input-that-dynamically-creates-additional-inputs-is-selected) {
    
    switch (dynamic-input-value) {
      
      // Generate whatever you need for further inputs.
      
    }
    
    if (dynamically-created-inputs-are-selected) {
      
      // Validate.
      
      // Rinse and repeat as necessary
      
    }
    
  }
  
}

// Render form with sticky values if not yet completed.
Link to comment
Share on other sites

Thanks for the advice. I'll give that a go tomorrow.

 

One thing I find frustrating as a part-timer at this is how often I spend hours, if not days on a script and get 3/4 way through it and find it won't work because I've got the logic wrong. Do you do an outline like you have above before you start a script or or from experience can you just go from top to bottom?

 

Paul

Link to comment
Share on other sites

You always need an outline (even if it's just five minutes thinking about the problem before you start), but in my experience, no matter how hard to try to predict everything, something will always come up that you don't expect, and you will have to change something around as a result.

With that said, having made all the mistakes you have made at one time myself, you eventually learn, and the culmination of those experiences leads to more or less knowing how to solve most problems out of the box.

Link to comment
Share on other sites

 Share

×
×
  • Create New...