dsapkqaduj6718493 2015-11-12 19:51 采纳率: 100%
浏览 44
已采纳

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.

  • 写回答

1条回答 默认 最新

  • dsff788655567 2015-11-12 20:27
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面