Remove previous input when renewing a webpage

Is there some PHP or javascript way of getting rid of previous input when I renew a webpage?

I have my little homework pages, nothing fancy, fill in the gaps, click some radio buttons, then click send.

If the student number checks out, the data is written to my database and a “success, your data has been saved” webpage is shown. Works well enough for my requirements.

If I click the browser “go back” button from the success page, I go back to the homework webpage and all the fields are filled in, I can send it again.

This is great when I am trying it out on my localhost. I make php display wrong answers. Sometimes there is a wrong answer in the answer key. I change the answer key, or address the problem some other way, until everything is correct.

But online, I would like to achieve the erasure of all fields if someone clicks the browser “go back” button.

The answers are saved in the browser somehow.

How can I delete them, so the naked webpage, bereft of answers, is displayed?

Hope I explained what I want clearly!!

Your form processing code and form should be on the same page. Upon successful completion of the form processing code (no user/validation errors), execute a redirect to the exact same url of the current page to cause a get request for that page. The will remove any $_POST data and cause your server-side code to output a form without any field values or selected/checked fields.

The code for any page should be laid out in this general order -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

Thanks for the reply.

I only learnt this from a book and what I could glean from the internet.

At the moment, I have webpage with a form. When a student clicks send, the form calls a php file in a folder below the folder with the webpage:

<form action="php/getcw21BE3wW13.php" method="POST" name="myForm" onsubmit="return checkForExpiration();" > 
<div id="div-sn">
<label for="sn"> Please enter your student number here:  <input TYPE="phone" pattern="\d{10}" NAME="sn" maxlength="10" size="10" required="required"> </label><br>
<label for="sname"> Please enter your name here:  <input type="Text" NAME="sname" maxlength="10" size="10" required="required"> </label>
</div><br>

That’s the only way I know to do it. You are saying, all the php in getcw21BE3wW13.php should be integrated in the same webpage?

Yes the form processing code and the form should be on the same webpage. Here’s an example, with just an email field -

<?php

// initialization
session_start();

$post = []; // array to hold a trimmed working copy of the form data
$errors = []; // array to hold user/validation errors

// post method form processing, detect if a post method form has been submitted
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	// trim all the data at once
	$post = array_map('trim',$_POST); // if any input is an array, use a recursive trim call-back function here instead of php's trim
	
	// validate inputs
	if($post['email'] === '')
	{
		$errors['email'] = "Email is required.";
	}
	else if(!filter_var($post['email'], FILTER_VALIDATE_EMAIL))
	{
		$errors['email'] = "Email format is not valid.";
	}
	
	// if no errors, use the form data
	if(empty($errors))
	{
		// use the data in $post here...
	}
	
	// if no errors, success
	if(empty($errors))
	{
		$_SESSION['success_message'] = "Some success message.";
		die(header("Refresh:0"));
	}
}

// html document starts here...
?>

<?php
// output and clear any success message
if(isset($_SESSION['success_message']))
{
	echo "<p>{$_SESSION['success_message']}</p>";
	unset($_SESSION['success_message']);
}
?>

<?php
// output any errors
if(!empty($errors))
{
	echo "<p>".implode('<br>',$errors)."</p>";
}
?>

<?php
// output the form, repopulating any field values with the existing form data
?>
<form method='post' autocomplete='off'>
<label>Email: <input type='email' name='email' value='<?=htmlentities($post['email']??'',ENT_QUOTES)?>'></label><br>
<input type='submit'>
</form>

An enhancement of this, when you have more than 2-3 fields, is to use a data-driven design, where you have a data structure (array, database table) that defines the fields, validation rules, and processing, that you use to dynamically validate and process the form data, rather than to write out bespoke code for each field.

I just tested the commonly suggested Post Redirect Get (PRG) practice used in this, to prevent the form from resubmitting data and it does work, but only for the last/single form submission. If there is more than one submission of the same form, such as when correcting multiple validation errors, the browser will offer to resubmit the invalid form data at the previous step(s) when using the back-button. If you do reload the page when offered to resubmit the form data, the form will get redisplayed with the old, invalid, values. It doesn’t appear that there is a 100% sure way of preventing the browser from caching these previously submitted sets of form data, except closing the browser, hence the common suggestion to always close the browser when using a public computer.

The above undesirable operation can be partially corrected by storing a value in a session variable that indicates the form has been completed (or not unsetting the success message and testing it), then not processing any of the resubmitted form data during the current browser session (the browser will still offer to do so when using the back-button.)

BTW - in your example markup, since the <label></label> tags are around the field, there’s no need for the for=’…’ attributes. If you did need the for=’…’ attributes, there must be a corresponding id=’…’ attribute, which your example doesn’t have.

Doing this -

and adding a test for !isset($_SESSION[‘success_message’]) to both the post method form processing conditional statement and around the display of the form, provides a reasonable solution.

Sponsor our Newsletter | Privacy Policy | Terms of Service