开发者

POST/REDIRECT/GET vs custom implementation

开发者 https://www.devze.com 2023-04-09 17:07 出处:网络
I have some forms, and am currently using an implementation as described below: <form action=\"/formpost.php\" method=\"post\" name=\"form1\" id=\"form1\">

I have some forms, and am currently using an implementation as described below:

<form action="/formpost.php" method="post" name="form1" id="form1">
    <input type="hidden" name="to" id="to" value="__COMMENT1" />
    <!-- rest of form -->
</form>

__COMMENT1 refers to the page where I want the user to be redirected after the form posts.

Pretty much what happens is that the form is posted to formpost.php, the $_POST array is converted to $_SESSION['POST'], the $_POST is unset, and then the user is redirected to the location referenced in the value of the hidden input field ([id = to] always the same ID/name.) I can then continue to use the user's form submitted values (referenced from the $_SESSION array) regardless of whet开发者_高级运维her they go 'back', refresh, etc.

Is there anything wrong with this?

Are there any benefits to using the POST/REDIRECT/GET pattern instead?


Nothing wrong with your method, it's more convenient than PRG. though, I see no point in keeping POST variables unless in case of error. and there is no point in unsetting $_POST, of course.

Well, after some explanation it seems that your setup is quite wrong.
there is no point in making single action for all forms as well as in unnecessary redirect.

make your form action the actual script that validates the form.

  • on success, redirect wherever you want.
  • on error:
    • save POST data and error messages into session and redirect to the same URL
    • populate the form and unset POST data and errors
    • show the form
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号