alternative to $_POST($_POST 的替代品)
问题描述
我有一个巨大的表单输入类型(文本、复选框、隐藏等).表单输入的内容取自数据库.用户必须进行一些更改并将数据保存回数据库.
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.
此时我正在使用一个具有 foreach($_POST as $key=>$value)
循环的函数.如您所知,post 方法存在问题:
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:
- 无法刷新,
- 不能倒退.
我想使用 $_GET
方法,但我的变量和值的长度大于 2000 个字符.
I'll like to use $_GET
method, but the length of my variables and values are bigger than 2000 characters.
你对我有什么建议,我能做什么?也许使用 $_GET
有一些技巧.也许我不明白如何正确使用它?
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?
推荐答案
POST 本身绝对没有问题.你只需要正确使用它
HTTP 标准规定您应该在收到 POST 请求后进行 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
header("Location: ".$_SERVER['PHP_SELF']);
exit;
处理您的表单后将解决您所有的问题"
after processing your form will solve all your "problems"
如果你想处理 post 错误,你可以使用 POST/Redirect/GET 模式.但是它不会在错误时重定向,您提到的问题变得可以忽略不计.
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.
这是一个简洁的例子:
<?
if ($_SERVER['REQUEST_METHOD']=='POST') {
//processing the form
$err = array();
//performing all validations and raising corresponding errors
if (empty($_POST['name']) $err[] = "Username field is required";
if (empty($_POST['text']) $err[] = "Comments field is required";
if (!$err) {
//if no errors - saving data and redirect
header("Location: ".$_SERVER['PHP_SELF']);
exit;
} else {
// all field values should be escaped according to HTML standard
foreach ($_POST as $key => $val) {
$form[$key] = htmlspecialchars($val);
}
} else {
$form['name'] = $form['comments'] = '';
}
include 'form.tpl.php';
?>
出错时,它会返回表单.但在成功提交表单后,它也会重定向.
on error it will show the form back. but after successful form submit it will redirect as well.
这篇关于$_POST 的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:$_POST 的替代品
基础教程推荐
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01