Page resubmission, simple problem

I am a beginner and am having problem with a form, when i refresh page with the form and the data, all my error messages are still there…

is there a way to stop the form repeating the same action when a page is refreshed, e.g. just going back to being empty…?

But what I do normally is, the form is submitted in the same page (generally in case of single entry system) checking whether the request method was posted or not. Validate entries and if there are any errors remain in the same page. If successfully done the processing form then redirect to the same page or another page as required:

    $errors = array();
    $email = mysql_real_escape_string($_POST['email']);
    if(empty($email) || !validateEmail($email)){
        $errors['email'] = 'Invalid E-mail';
        // do some actions
        // and redirect to the requried page or the same page form.php
        header("Location: form.php");
// if there are validations errors then the $error variable will have your messsages 
// which you can print out in your form
// put your rest of the form and/or other HTML tags here under.

The form will look something like this:

<form name="frm1" id="frm1" method="post" action="">
    E-mail: <input type="text" name="email" id="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : ''; ?>" />
    <?php if(isset($errors['email'])): ?><span style="color: red;"><?php echo $errors['email']?></span> <?php endif; ?>

hey thanks… is there any sample code you think could work for the PRG pattern?

Post/Redirect/Get pattern.


<input id="email" name="email" value="<?php echo isset($_SESSION['email']) ? htmlspecialchars($_SESSION['formdata']['email']) : "default value" />

<?php if (isset($_SESSION['formerror']['email'])): ?>
<strong>Please enter your email</strong>
<?php endif ?>

<?php unset($_SESSION['formerror'], $_SESSION['formdata']) ?>


if (!isset($_POST['email']) or !validateEmail($_POST['email'])) {
    $_SESSION['formerror']['email'] = TRUE;
    $_SESSION['formdata']['email'] = isset($_POST['email']) ? $_POST['email'] : "";

    header("Location: /form.php");

// do stuff

header("Location: /success.php");

That’s about it. This is a very simplified example to get you going. There’s a lot of manual work going on in this example which can be automated.

Bear in mind I’ve ommited a lot of stuff (session starting, declaring variables, etc.) for clarity.