开发者

PHP Sessions and have "prev" and "next" button and retain values

开发者 https://www.devze.com 2023-03-29 15:31 出处:网络
I have a multi-page form. I\'d like to have a \"prev\" and \"next\" button on page 2, 3 and 4 so that if an user in the开发者_如何学C middle of filling out page 2 can decide to return to page 1 to ed

I have a multi-page form.

I'd like to have a "prev" and "next" button on page 2, 3 and 4 so that if an user in the开发者_如何学C middle of filling out page 2 can decide to return to page 1 to edit/enter something and be able to still see values on page 2 where he left off.

Is this possible? any pointers/links would be immensely helpful.

(Yes, I have sessions and I can see the sessions after printr but if Im in the middle of page 2 and click backwards, I still dont see these sessions).

Thanks in advance.


Start the session when the user hits page 1. Each page should look in the session to see if there's any data there for that page, and if so, render the form with those values pre-filled. When the user hits either PREV or NEXT, process the form as if they hit submit, but instead of saving the values to (for example) a database, save them to the session. Then redirect to the requested NEXT/PREV page. When the user hits DONE (or whatever) on the last page, pull all the values from the session and process them to your database (or whatever.)


For a simple approach to a "multi-step" form, simply use Javascript. That is what I do.

However, if you want to have a stateful form that remembers data in between pages, you will have to use a session array to track the values entered.

When you fill out a page, the POST array is populated on page2. Serialize the array of post data and store it in a session array. Back on page1, if that session array is set, unserialize the data and echo the values into the right form element.


  1. You can save all of the data in a database as they move to a different page as well as print these values out when the page is loaded. This would still have to be sent to the server via AJAX in the case of a "Previous" link click.

  2. You can put all pages in one html page, then just hide/show the correct pages when they navigate using javascript.

  3. Save all entered data in a session, which would have to be sent to the server via AJAX if it is not submitted through a form the traditional way.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号