在会话中存储复选框 [英] Store check checkboxes in session

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

问题描述

这是我的代码

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

推荐答案

好,下面是一个工作示例

Okay, Here is a working example of what your trying to accomplish using a session.

<?php 
session_start();
if(isset($_GET['checkbox']))
{
    $_SESSION[$_GET['checkbox'][0]] = isset($_SESSION[$_GET['checkbox'][0]]) ? +!($_SESSION[$_GET['checkbox'][0]]) : TRUE;
    print_r($_SESSION[$_GET['checkbox'][0]]);
    die;
}
?>
<html>
    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    </head>
    <body>
        <table class='block'>
             <tr>
                 <td>
                     <input class='c1' type='checkbox' lid='checkbox1' <?php echo (isset($_SESSION['checkbox1'])) ? 'CHECKED="CHECKED"' : ''; ?>><label>checkbox1</label>
                 </td>
                 <td>
                     <input class='c2' type='checkbox' lid='checkbox2' <?php echo (isset($_SESSION['checkbox2'])) ? 'CHECKED="CHECKED"' : ''; ?>><label>checkbox2</label>
                 </td>
                 <td>
                     <input class='c2' type='checkbox' lid='checkbox3' <?php echo (isset($_SESSION['checkbox3'])) ? 'CHECKED="CHECKED"' : ''; ?>><label>checkbox3</label>
                 </td>
                 <td>
                     <input class='c2' type='checkbox' lid='checkbox4' <?php echo (isset($_SESSION['checkbox4'])) ? 'CHECKED="CHECKED"' : ''; ?>><label>checkbox4</label>
                 </td>
                 <td>
                     <input class='c3' type='checkbox' lid='checkbox5' <?php echo (isset($_SESSION['checkbox5'])) ? 'CHECKED="CHECKED"' : ''; ?>><label>checkbox5</label>
                 </td>
                 <td>
                     <input class='c3' type='checkbox' lid='checkbox6' <?php echo (isset($_SESSION['checkbox6'])) ? 'CHECKED="CHECKED"' : ''; ?>><label>checkbox6</label>
                 </td>
             </tr>
         </table> 
    </body>
</html>

<script>
    $(document).ready(function() {
       $('input[type=checkbox]').on('change', function() {
           $.get("sefesf.php", { 'checkbox[]':$(this).attr('lid') });
       }) 
    });
</script>

瘦的

基本上,每次你选中一个框,GET请求被发送到自己,并做+!在会话上。这意味着相反。所以它会自动检查和取消选中会话变量。使用AJAX异步地完成请求,并存储数据。 CHECK功能在服务器端完成,PHP在页面加载时使用DOM中的简单三元运算符。

Basically, each time you check a box a GET request is made to itself and does +! on the session. Which means the opposite of. So it will automatically check and uncheck the session variable. The request is done asyncronously with AJAX and stores the data. The CHECK feature is done server side with PHP on page load with a simple ternary operator in the DOM.

待办事项

确保您在_GET上添加了一些验证,您不想要存储异常数据。

Make sure you add some validation on the _GET, you dont want to be storing erraneous data.

此外,这是一个很肮脏的方式来做这种类型的事情,但一种方式无所事事。

Also, this is a pretty dirty way to do this type of thing, but a way none-the-less.

祝你好运,让我知道如果你有问题!

Good luck, let me know if you have questions!

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

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