You need to separate the concerns in your code. Processing a post method form is a different concern from producing the content for the page. You should also use a post method form for the 'forget' function, since it is altering the state/data on the server.
Your code should be arranged in the following general order -
Post method form processing goes near the top of your file.
If your form processing code needs to handle more than one action, use a switch/case statement as a simple controller.
Any validation errors should be stored as elements in an array variable, that also serves as an error flag. If the array is empty, there are no errors and you can use the submitted data.
After successfully processing any post method form data, with no errors, do a header redirect to the same page. this will cause a get request for the page - see this link for why you would want to do this - https://en.wikipedia.org/wiki/Post/Redirect/Get
To (optionally) display a 'success' message, store it in a session variable, then test for that session variable in the get method code. Clear the session variable after displaying the message so that the message will only be displayed one time.
If there are any errors, don't do the redirect, stay on the page, display the error messages, re-display the form, and re-populate the form fields with any submitted data.
The get method code goes next - this code gets/produces the data needed to display the page and decides what to display on the page.
Your code would look something like this -
<?php
$data = array(); // holds the 'current' data, either from the database table (not used in this example) or from the form submission
$errors = array(); // an array to hold error messages
// post method form processing
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$data = $_POST; // copy the post data to a common variable so that ALL the rest of the code operates on it regardless of where it came from
// if the code needs to handle more than one form, you can use a switch/case statement as a simple controller
switch($_POST['action'])
{
case 'send_info':
// validate all the input data here, setting errors in the $errors array
// you would have logic here that tests if the required inputs are empty or not
if(empty($data['forename']))
{
$errors[] = 'The Forename is required.';
}
if(empty($data['location']))
{
$errors[] = 'The Location is required.';
}
// if there are no validation errors, use the submitted data
if(empty($errors))
{
storeinfo();
}
break;
case 'forget':
resetinfo();
break;
// code for other post actions goes here...
}
// done with form processing
// after successfully (no errors) processing any post data, do a header() redirect to the exact same url that the form submitted to. this will cause a get request for your page. this will cause the browser to forget that a form was submitted and it won't try to resubmit the form data if you refresh the page or browse back to the same url. this also enforces separation of concerns. post method form processing, which modifies data on the server is a separate concern from displaying data due to a get request for your page. if you want to display a one-time 'success' message after the header() redirect, pass it in a session variable, then clear he session variable after the the message gets displayed.
if(empty($errors)){
header("Location: http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
die;
}
// if there are errors while processing any post data, you would not redirect, stay on the page, let the rest of the code on the page display the errors, (re)display the form, and repopulate the form fields with the previously submitted values.
}
// get method code
if(isset($_COOKIE["forename"]) && isset($_COOKIE["location"])){
// display the content
displayPage();
} else {
// display the form
if(!empty($errors))
{
// display the errors
// note: when validating user input, you should output a specific error message for each problem
// you would also populate the form field values from the $data array so that the user doesn't need to keep filling in the same data any time an error occurs and this will also allow you to 'edit' existing information
displayForm("please fill all fields to continue!");
} else {
displayForm();
}
}