简体   繁体   中英

having trouble destroying a session variable

I want to unset a session variable when a popup is closed using jQuery. The jQuery code is as follows:

$("#close").click(function (){
    $("#popup_container2").css("display", "none");
        if($("#popup_container2").css("display")=="none")
        {
            var s=1;
            $.post("check_data.php", "s=" + s, function(respose){
            location.reload();
        });
        }                   
});

This popup block will be opened when the page loads only if $_SESSION['question_temp_id'] is set. Else it won't. For this, I have added this code in the body tag.

if(isset($_SESSION['question_temp_id'])) {
    echo 'onLoad="div_show();"';
}

On the check data page, the php code is as follows.

if(isset($_POST['s']) && $_POST['s']==1) {
    unset($_SESSION['question_temp_id']);
}

The problem is that popup still opens even though I have unset that session variable. I can't figure out the problem here. Please suggest.

Thanks in advance.

  1. Debug the check data page. Possibly the unset is not being called.

if(isset($_POST['s']) && $_POST['s']==1) { unset($_SESSION['question_temp_id']); }

  1. My suggestion is to use actual value of question_temp_id instead of set/unset. You may use Y/N value. Though set/unset should work, comparing values generally gives better control to later changes in code.

Actually, the problem was that I hadn't used session_start in the start. Since there was a lot of code, so it was difficult to find the problem. However, later I found it out and corrected it and now, code works fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM