开发者

Upon validation failure, setting variables in origin page using PHP?

开发者 https://www.devze.com 2023-02-12 09:46 出处:网络
I have a form that has server-side validation with PHP. The form\'s action has all of the validation rules, and if there is any validation problems, the array $validation holds all of the errors. If t

I have a form that has server-side validation with PHP. The form's action has all of the validation rules, and if there is any validation problems, the array $validation holds all of the errors. If the array has a size (i.e. there were validation problems) then the script goes back to the origin page. The only thing is - I don't know how to carry variables in that direction. 开发者_运维知识库I can $_POST them from the origin to the post page, obviously, but I can't do the reverse.

How would I do that?


You can store the error messages within a session, which would then be present for all subsequent page loads. The down side that without some care and attention, if the user has multiple copies of the form open, submitting copy A will get the errors from copy B, and vice versa.

The other option is have your server-side processing redisplay the form if there's any errors:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ... validate form ...
   if ($form_has_errors) {
      redisplay_form();
   } else {
      ... do whatever has to be done with a 'good' submission ...
   }
}
?>
0

精彩评论

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