$_POST 的替代方案

发布于 2024-12-06 06:33:56 字数 329 浏览 0 评论 0原文

我有一个巨大的表单,其中包含类型的输入(文本、复选框、隐藏等)。表单输入的内容取自数据库。用户必须进行一些更改并将数据保存回数据库。

目前我正在使用一个具有 foreach($_POST as $key=>$value) 循环的函数。如您所知,post方法存在问题:

  1. 无法刷新、
  2. 无法后退。

我想使用 $_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:

  1. can't make refresh,
  2. 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 技术交流群。

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

发布评论

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

评论(3

停顿的约定 2024-12-13 06:33:56

使用 Post/Redirect/Get 模式。

Use the Post/Redirect/Get pattern.

眼眸印温柔 2024-12-13 06:33:56

POST 本身绝对没有问题。您只需正确使用
HTTP 标准规定您应该在收到 POST 请求后进行 GET 重定向。
简单代码

    header("Location: ".$_SERVER['PHP_SELF']);
    exit;

因此,处理表单后的

将解决您的所有“问题”,如果您想处理发布错误,您可以使用 POST/Redirect/GET 模式。
但是,它不会在错误时重定向,您提到的问题可以忽略不计。

这是一个简洁的示例:

<?  
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';
?>  

出错时它将显示回表单。但在成功提交表单后,它也会重定向。

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"

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:

<?  
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.

信愁 2024-12-13 06:33:56

就循环处理而言,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

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