How to transfer data from one PHP page to another
If you are starting out in PHP and just learning the basics, you’ll probably just start with one PHP page, but pretty soon you will want to add a second page. If you have a form on the first page, how do you pass that data to a second page.
This was illustrated recently by a question from a reader who emailed me to help with this problem:
Dear Jon, I’m struggling with getting data in my survey from one page to the next page in the survey, I don’t want to have one massively long page, so wanted to break the survey up into multiple pages, but then I need to store or pass the user data entered from one page to the next and finally to the end to save it, any ideas?
In PHP you have three options, but typically you will probably end of using all three options:
- POST / GET
Sessions are great to use if you aren’t passing a lot of data.
They do use up memory on the server however, so in the above case if you have a large survey and multiple users are using the application, a lot of memory could get consumed.
Databases are great for saving the data and having it persist beyond the end of the session.
POSTing the data between forms and storing it in hidden fields is another option, but again you may end with a large amount of data being posted between multiple pages.
The best option for a multipage survey will be to store it in the database as each page is submitted.