如何重置使用“粘性"的表单价值观 [英] How to reset a form that uses "sticky" values

查看:35
本文介绍了如何重置使用“粘性"的表单价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我在输入中使用 value="<?php echo $_SESSION['myVar']; ?>" 以便这些值是粘性"的在服务器端验证等期间重新填充值的目的.我的问题是如何使重置按钮 (type="reset") 实际重置表单并删除保存的值.我正在寻找一种不使用 javascript 如果可能的解决方案!谢谢!

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!

表格:

<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>

表单处理程序:

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

推荐答案

如果您不想使用 javascript,则需要重新加载页面.将此按钮设为 reset_session.php 的链接,您只需在其中写入

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'] = "";

然后使用 header() 重定向回表单.如果您想停留在某个页面上,可以向同一页面 ( reset_session.php ) 发送 ajax 请求,然后更新输入中的值.

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.

这篇关于如何重置使用“粘性"的表单价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆