Submitting Forms

I know this is ultra basic but I am sure there is a better way than how I do it.

I always submit the form to the same page:



<?php
$error = "";

if(isset($_POST['submit'])) {

  //check form

  if($formOK==1) {
     //add record and redirect 
  } else {
    //allow page to continue so display form again
   $error = "Not completed";
  }

}

echo $error;

?>
<form>

<input type="text" name="name" value="<?php echo $_POST['name']; ?>">

</form>

…so the above always shows the values entered in the form

The problem is I get the web-page expired problem.

If I header redirect after form submit (either another .php page or the same one) I then lose ability to display values submitted in $_POST.

What do I do to get $_POST values but no web-page expired messages?

Is this the best way to do it?

I suppose I then need to clear/unset sessions as needed.

Are there alternatives?

How do most experienced coders do it?

You could save the submitted value to a session;

$_SESSION['name'] = $_POST['name'];

Then insert the session variable into the form;

<input type="text" name="name" value="<?php echo $_SESSION['name']; ?>">

Sessions live longer than just the next page visit. There are more things to consider, but that should get you started.