0

I have been searching the interwebs for this, however, can't seem to find a solution. I have a PHP script that sets a $_SESSION['x'] and I display it on a page if it is set using a bootstrap alert. However, the darn thing never goes away and I can't figure out how to do it. I'm not trying to edit or insert anything into the variable simply just trying to unset it to "dismiss" the error.

Here is my code:

HTML page with PHP code to display error alert.

if(isset($_SESSION['x']) && !empty($_SESSION['x'])){
    echo "<div class='alert alert-danger alert-dismissible fade show' role='alert'>" . $_SESSION['x']. " Error: " . $_SESSION['errorCode']."
            <button id='close' type='button' class='close' data-dismiss='alert' aria-label='Close'>
                <span aria-hidden='true'>&times;</span>
            </button>
        </div>";
}

JS File

$("#close").click(function(){
    $.get("../php/scripts/clear/employeeError.php");
});

PHP File to clear variable

session_start();

if(isset($_SESSION['x'])){
    unset($_SESSION['x']);
}
echo $_SESSION['x']; // Just to see if it unsets

What I want to happen is when I click on the dismiss button to unset $_SESSION['x'] while not refreshing the page. Once the dismiss button is clicked the alert will dismiss on the current page and when you go back to the page later, the $_SESSION['x'] is unset so you don't see the error again.

  • Possible duplicate of [Set Session variable using javascript in PHP](https://stackoverflow.com/questions/3590293/set-session-variable-using-javascript-in-php) – 31piy Dec 20 '17 at 06:49
  • 1
    `employeeError` or `x`, which one do you want to delete? – Tuğca Eker Dec 20 '17 at 06:50
  • I think you're missing some key-points about life-cycle of application. Once you rendered your HTML page, any change on `$_SESSION` won't affect your already rendered page. You should refresh whole page, or alternatively you can render it again partially (run an ajax call and update related section of the page). – Tuğca Eker Dec 20 '17 at 06:58
  • @TuğcaEker My apologies, they are the same I changed it to make it more simple to post here. – AchiliesFury Dec 20 '17 at 06:59
  • @TuğcaEker Even with a refresh the session doesn't clear. – AchiliesFury Dec 20 '17 at 07:00
  • You simply need to use `unset` to clear whatever you want to.. From your code, it's unclear on whether are you trying to unset `x` or `employeeError` or `errorCode` – Milan Chheda Dec 20 '17 at 07:04

2 Answers2

0

I think you're pointing out some key concepts about server side and client side programming. You're using PHP to render HTML page, after rendering any change on PHP environment won't affect your already rendered page.

Please see;

How to assign php variable in JavaScript value assign?

set or change php variable in javascript

Tuğca Eker
  • 1,493
  • 13
  • 20
  • Updated bottom of the question to reflect what I am trying to accomplish. – AchiliesFury Dec 20 '17 at 07:06
  • My answer explains why can't you do that `while not refreshing the page.` But after refresh it should work again. Did you check your Development Console? Is this request successfull (200 OK) or not? – Tuğca Eker Dec 20 '17 at 07:07
  • Right, I understand that once the page is rendered it will not change anything. I am using Bootstrap to display the alert, which uses js to dismiss the alert when you click on the dismiss button. However, I also want it unset the Session so when you do reload the page it will not display. – AchiliesFury Dec 20 '17 at 07:12
  • OK, I understood. Did you check the response of your network request? [See this.](https://developers.google.com/web/tools/chrome-devtools/network-performance/reference) – Tuğca Eker Dec 20 '17 at 07:13
  • I'm getting a status of 304 – AchiliesFury Dec 20 '17 at 07:16
0

Here is what I have figured out to work

$(function () {
    $('.close').click('.close', function(){
        $.get('php/scripts/clear/employeeError.php', function(data){
            alert("Server Returned: " + data); //Used to verify script runs
        });
        return false;
    });
}); 

Basically what happens now is I use this inline script to listen for the .close and then it will call the script to unset the session variable.