$_POST 的替代方案
我有一个巨大的表单,其中包含类型的输入(文本、复选框、隐藏等)。表单输入的内容取自数据库。用户必须进行一些更改并将数据保存回数据库。
目前我正在使用一个具有 foreach($_POST as $key=>$value)
循环的函数。如您所知,post方法存在问题:
- 无法刷新、
- 无法后退。
我想使用 $_GET
方法,但我的变量和值的长度超过 2000 个字符。
您对我能做什么有什么建议吗?也许使用$_GET
有一些技巧。也许我不明白如何正确使用它?
I have a huge form with inputs of type (text, checkboxes, hidden et). The content of the form inputs are taken from a database. The user has to make some changes and to save the data back into the db.
At this moment I'm using a function which has a foreach($_POST as $key=>$value)
loop. As you know, there are problems with post method:
- can't make refresh,
- can't go backwards.
I'll like to use $_GET
method, but the length of my variables and values are bigger than 2000 characters.
Do you have any advice for me, about what can I do? Maybe there are some tricks in using $_GET
. Maybe i didn't understand how to use it right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Post/Redirect/Get 模式。
Use the Post/Redirect/Get pattern.
POST 本身绝对没有问题。您只需正确使用
HTTP 标准规定您应该在收到 POST 请求后进行 GET 重定向。
简单代码
因此,处理表单后的
将解决您的所有“问题”,如果您想处理发布错误,您可以使用 POST/Redirect/GET 模式。
但是,它不会在错误时重定向,您提到的问题可以忽略不计。
这是一个简洁的示例:
出错时它将显示回表单。但在成功提交表单后,它也会重定向。
There is absolutely nothing wrong in POST itself. You just have to use it properly
An HTTP standard says you ought to make a GET redirect after receiving POST request.
So, as easy code as this
after processing your form will solve all your "problems"
in case you want to handle post errors, you can use POST/Redirect/GET pattern.
However it does not redirect on error, the problems you mentioned becoming negligible.
here is a concise example of it:
on error it will show the form back. but after successful form submit it will redirect as well.
就循环处理而言,PHP 中的 foreach 循环结构会复制您正在处理的数组。如果您想使用不同的循环结构(for / while)处理 $_POST 并使用 count()、current()、reset()、next()、prev()、end()、each() 或key(),开始吧。
PHP 编程:第 5 章,第 14 页128-129
As far as loop processing goes, the foreach loop contruct in PHP makes a copy of the array you are working on. If you want to process $_POST with a different loop construct (for / while) and use functions like count(), current(), reset(), next(), prev(), end(), each(), or key(), have at it.
Programming PHP: Chapter 5, p. 128-129