Is it ok or standard to use $_SERVER['PHP_SELF'];
and header('Location: http://www.example.com/form.php');
in the same page to avoid resubmitting the form on refresh.
I understand the concept of using $_SERVER['PHP_SELF'];
on a page with a form, but then if I use header('Location: http://www.example.com/form.php');
to avoid resubmitting to the db should I be redirecting to the same page.
I'm kinda confused.
UPDATE: Making it a bit more clear
I don't want to use the processing file name in form action (future proof - filename change, etc.), hence the use of $_SERVER['PHP_SELF']
but now hitting refresh resubmits the form. To avoid that we go the PRG route, but redirecting it to the same page $_SERVER['PHP_SELF']
is not solving the issue.
I wonder how others are doing it. Bottom line, I want to have my form and processing it in the same page without resubmission on refresh.
This pattern is known as Post/Redirect/Get.
You should redirect to that page that processes the input data, for example if your form action is $_SERVER['PHP_SELF'];
you process the input data on the same page, so you redirect to the same page, if you want to process the input data on another page, than redirect to that page.
So, if
should I be redirecting to the same page.
is a question. the answer is yes. And you maybe want to add some GET
variables to show a "successfully sent" message, or something like that.