问题
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.
回答1:
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!
回答2:
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.
Solution of your problem
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.
Problem 1 : You are not using GET method to store value into Session variable
<?php
if(!isset($_SESSION))
{
session_start();
}
if(!isset($_GET['x'])) $_GET['x'] = '';
$_SESSION['x'] = $_GET['x']; // storing data using GET method
?>
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.
Problem 2 : You are not retrieving Session variable
<script type='text/javascript'>
$('input[type=checkbox]').each(function (){
if (this.checked) {
var x = $(this).attr('lid');
}
});
</script>
In your javascript
code, you didn't mention any session variable, by which, you can match the previous checked variable.
来源:https://stackoverflow.com/questions/13991092/how-to-store-checkboxes-states-in-session