How to reset a form that uses "sticky" values

Go To


I have a form where I am using value="<?php echo $_SESSION['myVar']; ?>" within the input so that the values are "sticky" for purposes of repopulating the values during server side validation, etc. My question is how do I make the reset button (type="reset") actually reset the form and remove the saved values. I am looking for a solution that doesn't use javascript if possible! Thanks!


  <label class="label1" for="name"> Name: </label>
  <input class="input1" type="text" name="name" id="name"  maxlength="40" value="<?php echo $_SESSION['name']; ?>" />
  <label class="label1" for="email"> Email: </label>
  <input class="input1" type="text" name="email" id="email"  maxlength="40" value="<?php echo $_SESSION['email']; ?>" />
  <label class="label1" for="message"> Message: </label>
  <textarea class="flat input1" name="message" id="message" rows="5" cols="40"><?php echo $_SESSION['message']; ?></textarea>


//save user entered values
$_SESSION['name'] = trim($_POST['name']);
$_SESSION['email'] = trim($_POST['email']);
$_SESSION['message'] = trim($_POST['message']);
2012-04-03 21:41
by Brad Givens
Any non standard client side interaction is going to be next to impossible to do without JS - BenOfTheNorth 2012-04-03 21:44
I agree, it would be nice to have an <input type="clear"> or similar, but you either have to use JavaScript or refresh the page and have the PHP code clear out those variables - jnylen 2012-04-03 21:48
What @BenGriffiths said. Most websites nowadays operate under the assumption that everyone has JS enabled anyways, including Stack Overflow itself. I think it's a safe assumption too - NullUserException 2012-04-03 21:48


if you do not want javascript, you would need to reload the page. Make this button a link to reset_session.php, in which you will just write

$_SESSION['name'] = "";
$_SESSION['email'] = "";
$_SESSION['message'] = "";

and then redirect with header() back to the form. If you would like to stay on a page, you could send an ajax request to the same page ( reset_session.php ) and then update values in inputs.

2012-04-03 21:45
by mkk
This is the only way to do it without JavaScript (requires a page reload) - jnylen 2012-04-03 21:47


You need to use JavaScript for that. Reset will always reset to the original values the form had when the markup loaded.

2012-04-03 21:44
by Francisc


The Reset button will reset the form to the values inside of the value attributes. And that's it. As you populate those values, it will always reset to these values.

If you want to make it reset to blank values, don't populate / pre-fill those values.

2012-04-03 21:44
by hakre


if you use only php, you can try with: unset($_SESSION['myVar']); or session_destroy();

2012-04-03 21:46
by gyula.nemeth