简体   繁体   English

按下浏览器后退按钮时如何保持收音机的选定状态?

[英]How to keep radio selected states when hitting browser back button?

I am handling a survey multistep form with many radio sets. 我正在处理带有许多收音机的调查多步骤表单。

They have some default states 他们有一些默认状态

User operates over them to select his preferences. 用户对其进行操作以选择其首选项。

Now the client wants when hitting either browser back button or the side navigator links (a set of links pointing to previous stages of the survey) to have the radio states the user previously chose. 现在,客户希望在单击浏览器后退按钮或侧面导航器链接(指向调查的前几个阶段的一组链接)时具有用户先前选择的无线电状态。

Here is how the navigator looks like: 导航器的外观如下:

<ul>
<li><a href="index.php?site=form1">Step1</a></li>
<li><a href="index.php?site=form2">Step2</a></li>
<li><a href="index.php?site=form3">Step3</a></li>
</ul>

There is only one $_SESSION that collects data throughout the survey. 在整个调查中只有一个$ _SESSION收集数据。

I know I can load information directly from $_SESSION but I need to replace the default states when the request comes from the navigator links/ back button. 我知道我可以直接从$ _SESSION加载信息,但是当请求来自导航链接/后退按钮时,我需要替换默认状态。

There are two main approaches to this, basically you are saying that the chosen state of the radio's need to be saved between different pages of your form. 有两种主要的处理方法,基本上是说要在表格的不同页面之间保存无线电的选定状态。

So when you return to a page you were on previously but without submitting, the choices should still be the same. 因此,当您返回以前的页面但未提交时,选择应该仍然相同。

There are two key approaches, server side with PHP or client side with Javascript 有两种主要方法,服务器端使用PHP或客户端使用Javascript

PHP 的PHP

You can set any link on the website to submit the form and you could save the radio selections, then when you create the form you can check the previously selected value 您可以在网站上设置任何链接以提交表单,并且可以保存单选,然后在创建表单时可以检查以前选择的值

This information could be saved in $_SESSION but they'd need to submit the form each time they changed the page (you could change the links to do this) 此信息可以保存在$_SESSION但是他们每次更改页面时都需要提交表单(您可以更改链接来完成此操作)

Javascript Java脚本

You could write some javascript that remembered the content of the selections and stored them in the users browser as a cookie, this could update everytime they clicked a button. 您可以编写一些JavaScript来记住选择内容,并将它们存储为Cookie,并将其存储在用户浏览器中,这可以在他们每次单击按钮时进行更新。 When the page loads the javascript would check for cookies (or local storage) and would load the previous options. 当页面加载时,javascript将检查cookie(或本地存储)并加载先前的选项。

When they finish and submit the form you'd clear the javascript 当他们完成并提交表格后,您将清除javascript

Basic example 基本例子

$('form input[type=radio]:checked')

Once I have more information I'll flesh out this answer further 了解更多信息后,我将进一步充实此答案

After posting form, you can save values in the session. 发布表单后,您可以在会话中保存值。 When accessing previous page again (or even first load it), read session's values and check specified radios. 当再次访问上一页(甚至首先加载它)时,请读取会话的值并检查指定的无线电。 If you access page first time, there will be no data in the session, so no one will be checked. 如果您是第一次访问页面,则会话中将没有数据,因此将不会检查任何数据。

You can also store selected values lively with JavaScript and cookies. 您还可以通过JavaScript和cookie生动地存储选定的值。

A very simplified possible solution: 一个非常简化的可能解决方案:

<?php
   session_start(); // this needs to be at the top of all pages using $_SESSION (before anything else) else $_SESSION will always be empty
   if($_POST['vehicle']){
      $_SESSION['checkbox1'] = $_POST['vehicle'];
   }
print_r($_SESSION); // remove this line after testing. It will show you the contents of the $_SESSION, which is handy for seeing when variables are set within it
?>

<!DOCTYPE html>
<html>
<body>
<form action="" method="post">
   <input type="checkbox" name="vehicle" value="Bike"<?php if($_SESSION['checkbox1'] == "Bike"){ echo " checked"}?>>I have a bike<br />
   <input type="checkbox" name="vehicle" value="Car"<?php if($_SESSION['checkbox1'] == "Car"){ echo " checked"}?>>I have a car <br />#
   <input type="submit" value="Go" />
</form>
</body>
</html>

You could add a unique id generated at the first page in a hidden input area that when posted could form part of the session variable name. 您可以在隐藏输入区域的第一页上添加一个唯一的ID,该ID在发布时可以构成会话变量名称的一部分。 From there the information would be indexed in the $_SESSION under that variable name making it easier to pull the data back out or to create a new form session as it were. 从那里,该信息将在$ _SESSION中的该变量名称下建立索引,从而更轻松地拉回数据或按原样创建新的表单会话。

Hope I haven't lost you. 希望我没有失去你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM