如何在会话中存储复选框状态? [英] How to store checkboxes states in session?

查看:67
本文介绍了如何在会话中存储复选框状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

<table class='block'>
  <tr>
    <td>
      <input class='c1' type='checkbox' lid='checkbox1'>
        <label>checkbox1</label>
      </td>
    <td>
      <input class='c2' type='checkbox' lid='checkbox2'>
        <label>checkbox2</label>
      </td>
    <td>
      <input class='c2' type='checkbox' lid='checkbox3'>
        <label>checkbox3</label>
      </td>
    <td>
      <input class='c2' type='checkbox' lid='checkbox4'>
        <label>checkbox4</label>
      </td>
    <td>
      <input class='c3' type='checkbox' lid='checkbox5'>
        <label>checkbox5</label>
      </td>
    <td>
      <input class='c3' type='checkbox' lid='checkbox6'>
        <label>checkbox6</label>
      </td>
  </tr>
</table>
<script type='text/javascript'>
  $('input[type=checkbox]').each(function (){
  if (this.checked) {
  var x = $(this).attr('lid');
  }
  });
</script>
<?php
    if(!isset($_SESSION))
        {
            session_start();
        }
    if(!isset($_GET['x'])) $_GET['x'] = '';
    $_SESSION['x'] = $_GET['x'];
 ?>

我要在会话中保存选定的复选框。因此,当我返回页面时,应保持选中相同的复选框。

I want to save selected checkboxes in a session. So that, when I come back to the page, the same checkboxes should remain selected.

推荐答案

在我看来,而不是保存他们进入会话,将它们保存在数据库中。当您返回同一页面时,请从数据库中检索值。而且我相信,这将是更容易且最佳的方法。

In my opinion, rather than saving them into the session, save them in database. And when you came back on same page, retrieve the value from database. And i believe, it would be much easier and best approach.

解决问题的方法

首先,您需要确定您的数据/复选框值是否已保存到会话变量中。因为,经过代码段之后,我真的怀疑您的复选框值是否会保存在会话变量中。

First, you need to be sure, whether your data/checkbox value is being saved into session variable or not. Since, after going through your code-snippet, i really doubt that, your checkbox value would be saved in session variable.

问题1:您没有使用将值存储到会话变量中的GET方法

<?php
    if(!isset($_SESSION))
        {
            session_start();
        }
    if(!isset($_GET['x'])) $_GET['x'] = '';
    $_SESSION['x'] = $_GET['x'];  //  storing data using GET method
 ?>

这是您的代码,您尝试在其中使用 GET 方法。但是,我没有找到要传递所选值的任何表格

This is your code, where you are trying to store data in session variable using GET method. But, i didn't find any form by which you are passing the selected value.

问题2:您不是要检索会话变量

<script type='text/javascript'>
  $('input[type=checkbox]').each(function (){
  if (this.checked) {
  var x = $(this).attr('lid');
  }
  });
</script>

在您的 javascript 代码中,您没有不要提及任何会话变量,通过该变量,您可以匹配之前检查的变量。

In your javascript code, you didn't mention any session variable, by which, you can match the previous checked variable.

这篇关于如何在会话中存储复选框状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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