Since you're submitting the form
to the same page, the server will (after processing any PHP code on that page), display that page. You have a couple of options here:
- Submit the form to a different page.
- Redirect from the page after processing the form.
The first option would mean that you'd change the action
in the form
to something like this:
<form name="list" action="formhandler.php" method="post">
This would mean that formhandler.php
(or whatever you decide to call it) would have the server-side PHP code to process the form, and then that page would display whatever you want it to display.
The second option would involve just using a redirect in you PHP code (on the page to which you're already submitting) to direct the user to a different page after processing the form. The PHP header()
function is the standard way to do this. Something like this:
header("Location: thankyou.php");
This should essentially be the last thing the PHP code does after processing your form. That is, you probably don't want your server-side code to continue to execute after what would intuitively be a terminating case. So you'll want to call exit
immediately after it to stop page execution. (Keep in mind that the actual redirect isn't a server-side action, the server is just sending the Location
header to the browser and telling the browser that it should perform the redirect.)
(Also, as noted in other answers here, there should be otherwise no output to the page when performing such a redirect.)
Additionally, I suppose you could also have conditional output on your PHP page, displaying one set of HTML content (the form) if it's not handling a POST or another set of content (the thank you page) if it is handling a POST, but that feels like a slippery slope to me. It's better to separate your server-side resources than to have one named resource (somepage.php) that does different things conditionally. (Single Responsibility Principle)