PHP - 表单,发布到同一页面,错误处理,防止重新提交

PHP  - 表单,发布到同一页面,错误处理,防止重新提交

问题描述:

I have come across quite the problem when making my own framework. I have found some other questions that come close to what I'm asking, but not quite.

This is what I am attempting:

  • Page has a form with method post and submits to itself (same page).
  • When you submit, the input is validated appropriate messages / errors are displayed.

It seems simple, but I am trying to:

  • Avoid form resubmission that comes with using post. This wouldn't be a problem if I also wasn't trying to...
  • Avoid having duplication or more code than needed. Redirection adds an extra action where I would need to check the input twice (once on the submit page for validation and once on the redirected page for messages/ errors).
  • Avoid using query strings, cookies or sessions. Which makes the solution of posting to another page and then redirecting while still having message handling impossible (as far as I know).
  • Avoid relying on AJAX since JS can be disabled.

Am I asking too much or is there a way to do this? Is there something I would need to compromise on to achieve this? If not, then I guess I will go with sessions and redirection.

在制作自己的框架时,我遇到了很多问题。 我发现了其他一些问题,这些问题与我提出的问题相近,但并不完全正确。 p>

这就是我正在尝试的问题: p>

    \ n
  • Page有一个带有方法发布的表单并提交给自己(同一页面)。 li>
  • 当你提交时,输入被验证相应的消息/错误 显示。 li> \ n ul>

    看起来很简单,但我想: p>

    • 避免使用post附带的表单重新提交。 如果我也没有尝试,这不会是一个问题...... li>
    • 避免重复或代码超出需要。 重定向添加了一个额外的操作,我需要检查输入两次(一次在提交页面上进行验证,一次在重定向页面上查看消息/错误)。 li>
    • 避免使用查询字符串,cookie或 会话。 这使得发布到另一个页面然后重定向,同时仍然无法处理消息处理(据我所知)。 li>
    • 避免依赖AJAX,因为JS可以被禁用。 li> ul>

      我问得太多了还是有办法做到这一点? 有什么我需要妥协才能实现这一目标吗? 如果没有,那么我想我会选择会话和重定向。 p> div>

i am not sure i understood well, do you need to process $_POST on the same page, which holds the form and avoid re-sending the data?

if so, u need to reload page, without POST after proceeding your form ... i.e.

 if(isset($_POST["YOUR_FORM_FIELD"])){
   header("Location: ".$_SERVER["HTTP_REFERER"]);
   exit();
 }

BUT - reloading is in conflict with your request to not use _SESSION, or _GET nad send a message to be displayed. i did not find a way how to pass a variable through HTTP headers - which seems to me like the only way ...

anyway i do recommend to consider using _SESSION ....