If you run and submit the form below:
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST">
First name:<br>
<input type="text" name="firstname" value="<?php if(isset($_POST['firstname'])){echo $_POST['firstname'];} ?>"
<br>
<input type="submit" value="Submit">
<input type="button" onclick="this.form.reset()" value="Clear Form">
</form>
</body>
</html>
Why does this.form.reset()
not work when the form is pre-populated with the previously submitted value? The Clear Form button only works before submitting data. I've tried a number of similar approaches using jQuery as well that also only work before submitting the form, never after.
Since JavaScript runs after server-side code, I would expect the value from $_POST['firstname']
to be replaceable with JavaScript (in this case, an empty string).