0

我有一个表单,当用户选中一个复选框时,我使用 JQuery post 方法设置了一个会话变量。

我的问题:如果用户返回并决定取消选中以前选中的框,是否有直接的方法来取消设置会话变量?

HTML

<?php session_start() ?>
<html>
<head>




<script type="text/javascript">
        $(document).ready(function(){
var checkboxes = $(".chkbx").change(function() {
    var allIsChecked = checkboxes.length === checkboxes.filter(":checked").length;
    all[0].checked = allIsChecked;
    enableMyElement(checkboxes.filter(":checked").length > 0);
    putInSession($(this).val());
});
var all = $("#checkall").change(function() {
    checkboxes.attr("checked",this.checked);
    enableMyElement(this.checked);
});


  $("#myform").change( function () {    
              $.post(
               'actions.php',
               $(this).serialize(),
                function(data){
                  $("#results").html(data)
                }
              );
              return false;   
            }); 


  });
</script>

</head>


<body>
<form id="myform">
<input id="checkall" type="checkbox">
<input class="chkbx" type="checkbox" value="360" name="box1">
<input class="chkbx" type="checkbox" value="500" name="box2">
<input class="chkbx" type="checkbox" value="510" name="box3">
<input type="submit" value"Send" />
</form>

<div id="results">Will go here</div>

<?php print_r($_SESSION); ?>

</body>

</html>

处理脚本

<?php
session_start();
$post = $_POST;
foreach ($post as $key => $value) {
echo $key. "->" .$value; 
$_SESSION[$key] = $key;
}
?>
4

1 回答 1

0

是的。使用 jQuery 帖子传入复选框的状态(选中、未选中)。然后在处理脚本中,您可以检查它是否已检查并设置/取消设置$_SESSION变量。

if($_POST['checked']) {
    // your current foreach loop
} else {
    unset($_SESSION);
}
于 2013-02-13T21:53:17.387 回答