PHP: print success or error message on submit?
i wonder how i can solve that: i have a lot of forms on my website. whenever i submit a button i would like to print a message on the following page. when a form is submitted the page simply refreshes. what's the easiest way to do开发者_StackOverflow中文版 that or how is it normally done?
thanks for the help
Probably use PHP Sessions and add a session variable before the form refreshes, then retrieve it in the next page, show it and delete it.
e.g. in your redirect page
$_SESSION["message"] = "Success!";
then in your forms
if(isset($_SESSION["message"])) { echo $_SESSION["message"]; unset($_SESSION["message"]); }
This is the simple way to do it, and I do it like this. You might want to add a global template to your forms so that you can edit the second code globally.
This approach is good if you have different forms and if you redirect to pages that is not the original form, so you can show feedback messages globally.
You could use $_SESSION as Jimmie suggests. That wil work.
If you want to avoid that, then you process the input in your <form action=...
page. Process the form, decide if it's valid or not - before writing any output - and then output either a success or a failure page.
If you need to build the output page as you you are processing the form input form $_GET
or $_POST
then buffer the output (see http://php.net/manual/en/function.ob-start.php)
p.s what did you mean by when a form is submitted the page simply refreshes
? You are seeing the same inoput page again? If so, check that <form action=...
leads you to a second page.
HTH
First of all, I won't recommend to use the blank action. Anyways... if you are using it, then, while your are processing your form data, and checking the validation, you can store all the errors and success messages in an array, then if the action is blank, you will simply get them on the same page (if you don't send header or redirect the page), and if not, then you can store that array in the session, to get it in the next page... And finally you can unset that array
Hope this helps. Thanks.
精彩评论