-1

Also, how do i unset the session through javascript?

I have made this code with PHP:

if(isset($_SESSION) && !empty($_SESSION)) {
            unset($_SESSION['Plan1']);
            unset($_SESSION['excel_array']);
            $_POST['excel_name']['Main'] = "Main".date("y_m_d_Hi");
            $_POST['excel_array']['Main']['Plan1'] = array();
        }
        else{
            $_SESSION['excel_name']['Main'] = "Main".date("y_m_d_Hi");
            $_SESSION['excel_array']['Main']['Plan1'] = array();
        }

So here i check if there is a session. If there is, i unset it and send the $_POST data instead. however, if there isn't one, i create it.

The problem is, i might want to call this on a button click. How would i make a code with the same functionality, but using a javascript function?

5
  • 1
    tip: use ajax request Commented Sep 2, 2016 at 12:02
  • This doesn't make a lot of sense to me. What are you trying to do? Commented Sep 2, 2016 at 12:03
  • Why javascript, why not a form POST submit? Commented Sep 2, 2016 at 12:03
  • 1
    Your answer : stackoverflow.com/questions/11839079/… Commented Sep 2, 2016 at 12:04
  • PHP sets a PHP SESSION cookie. Simply delete this Commented Sep 2, 2016 at 13:16

1 Answer 1

1

Put your php in a file on its own, called set_session.php for example:

<?php
    session_start();
    unset($_SESSION['Plan1']);
    echo 'unset';
?>

Call it in javascript:

<script>
function unset() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("result").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "set_session.php", true);
  xhttp.send();
}
</script>

<button type="button" onclick="unset()">Unset</button>
<div id="result"></div>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.