Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Here is my code

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

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>

The Skinny

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.

To Do

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...