How to reset a form that uses “sticky” values

自作多情 提交于 2019-12-25 08:19:47

问题


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!

FORM:

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

FORM HANDLER:

//save user entered values
$_SESSION['name'] = trim($_POST['name']);
$_SESSION['email'] = trim($_POST['email']);
$_SESSION['message'] = trim($_POST['message']);

回答1:


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.




回答2:


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




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/10001844/how-to-reset-a-form-that-uses-sticky-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!