使用 SESSION 控制 PHP WebApp 状态

发布于 2024-11-17 21:18:08 字数 318 浏览 2 评论 0原文

我正在创建一个表单向导,要求用户在继续下一步之前逐步执行并完成每个步骤。

现在,一旦验证并完成了一个步骤,我就会将 $_SESSION['step'] 切换到下一个步骤,并在下一步加载时检查该变量以确保其有效。如果用户尝试跳到不按顺序的步骤,$_SESSION['step'] 会阻止加载该步骤。

这是正确的方法吗?此外,一旦完成一个步骤,我希望用户能够返回并编辑在任何给定的先前步骤中输入的值。我的方法不允许这样做,因为我不确定如何将会话更改为他们重新访问的步骤,而不知道它是否是有效的请求。

任何对此的意见将不胜感激!

I'm creating a form wizard that requires the user to step through and complete each step before moving on to the next.

Right now, once a step is validated and completed, I switch $_SESSION['step'] to the next step in line, and check that variable when the next step loads to make sure it is valid. If a user tries to skip to a step that is out of sequence, the $_SESSION['step'] prevents the step from loading.

Is this the proper approach? Also, once a step is completed, I want the user to be able to go back and edit the values that were entered on any given previous step. My approach doesn't allow for this, as I'm not sure how I'd set change the session to the step they are revisiting without knowing if it is a valid request or not.

Any input on this would be much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2024-11-24 21:18:08

这种方法没有任何问题,尽管您可能会受到一些限制,因为这不适用于多个表单。但是,如果这只是一个小应用程序,那就不用担心。

第二个问题我不确定我是否理解,但是 $_SESSION 可以接受任何数据类型,包括数组,因此只需在 $_SESSION 中填写您所在的位置以及到目前为止您在表单中所做的操作即可;

$_SESSION['my_form'] = array (
   'current_step' = 'step_2',
   'step_1' = array ( // the form elements ),
   'step_2' = array ( // the form elements ),
   'step_3' = array ( // the form elements ),
   'step_4' = array ( // the form elements ),
) ;

接下来,您将像往常一样在每个点进行验证。您还可以在每个表格中添加一个小标志,无论它们是否已填写;

   'step_2' = array (
      'complete' = true,
      // form elements / fields here
   ),

另一种划分方法是使用数千种动态表单中的一种。例如,使用 JQuery,您可以使用 Accordion 插件(来自 JQuery UI)在一个 HTML 页面中模拟整个过程。

There's nothing wrong with this approach, although you might be a bit limited in that this doesn't work for more than one form only. However, if this is just one small app, then no worries.

The second question I'm not sure I understand, but $_SESSION can accept any data type, including arrays, so just fill a $_SESSION with where you are and what you've done in the form so far ;

$_SESSION['my_form'] = array (
   'current_step' = 'step_2',
   'step_1' = array ( // the form elements ),
   'step_2' = array ( // the form elements ),
   'step_3' = array ( // the form elements ),
   'step_4' = array ( // the form elements ),
) ;

Next, you provide validation at each point as normal. You can also put a little flag into each form whether they have been filled out or not ;

   'step_2' = array (
      'complete' = true,
      // form elements / fields here
   ),

Another way to slice this is to use one of the many thousands of dynamic forms. For example, using JQuery, you can use the Accordion plugin (from JQuery UI) to simulate the whole thing in just one HTML page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文