使用 PHP 编写向导时的 URL 流程
我正在为我的网站编写一个基本向导。它将有 4 个步骤,每个步骤都需要有自己的 URL。每个步骤必须首先验证表单,然后才能继续。
如果给定步骤的表单无法验证,我不希望 URL 发生更改。但如果它通过了,我确实希望它继续下去。
写这个的首选方式是什么?单独使用 javascript 进行验证不够安全。到目前为止,我有 2 个想法,但我都不喜欢:
1)将表单发布到同一脚本,并使用 header() 重定向到下一步(如果通过)。
2)发送一个ajax post进行验证,然后使用location.href将用户发送到下一步(如果通过)。
有更好的方法吗?
谢谢,布莱恩
I am writing a basic wizard for my web site. It will have 4 steps, and each needs to have its own URL. Each step must first validate a form before moving on.
If the form for a given step fails to validate, I don't want the URL to change. But if it passes, I do want it to move on.
What is the preferred way to write this? Using javascript alone to validate is not secure enough. I have 2 ideas so far but I don't love either:
1) Post the form to the same script and use a header() redirect to the next step if it passes.
2) Send an ajax post to validate and then use location.href to send user to the next step if it passes.
Is there a better way to do this?
Thanks, Brian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我更喜欢选项#1,因为它不需要 javascript。
但是,您还需要考虑当有人添加书签或直接跳到错误的步骤时会发生什么。
I would prefer option #1, since it doesn't require javascript.
However, you'll also want to consider what happens when somebody bookmarks or skips directly to the wrong step.
你的选项#1正是我所做的。
Your option #1 is exactly the way I'd do it.
请记住,您可以结合这两种方法,以便:
您可以使用标准技巧来做到这一点
:将需要一些合格的工程,以便
ajax_handler()
可以利用与bar.php
相同的代码来处理表单数据。另外,您应该特别注意 AJAX 路径,以便后退按钮等内容继续按用户期望的方式工作。Keep in mind that you can combine these two approaches so that:
You would do that using a standard trick:
This will require some competent engineering so that
ajax_handler()
can utilize the same code asbar.php
does to process the form data. Also, you should pay special attention in the AJAX path so that things like the back button continue to work as the user expects.另一种选择是将所有验证逻辑(在此步骤之间)放在一个 PHP 页面中(然后总是进行
POST
编辑)。在回发时,对当前步骤进行验证并(仅在有效时)分支到下一步。您还需要在帖子之间保留“之前的”验证。
您仍然可以使用
myform.php?step=1
和myform.php?step=2
意义上的不同
URL 以及...通过一些简单的 url 重写,可以是myform/step1
、myform/step2
、...Another option would be to have all the validation logic (between this steps) in one single PHP page (which then always gets
POST
ed to).At postback, do the validation for the current step and (only if valid) branch out to the next step. You also need to persist the 'previous' validations between posts.
You can still have
different
URL's in the sense ofmyform.php?step=1
andmyform.php?step=2
and ... With some simple url rewriting that could bemyform/step1
,myform/step2
, ...