$_POST 的替代品 [英] alternative to $_POST

查看:46
本文介绍了$_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:

  1. 无法刷新,
  2. 不能倒退.

我想使用 $_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 的替代品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆