-1

I was trying to unset a php session variable inside a jQuery if block. Whenever someone unchecks a checkbox it should unset a specific session variable. Here is my code below

PHP

session_start();
$_SESSION['myName'] = "John";

HTML

<form action="" method="post">
  <input type="checkbox" id="myCheckbox"> Check to set session
</form>

jQuery :

$('form').change(function(){
    if($('#myCheckbox').is(':checked')){

        // Some statements here

    }else{
        <?php unset($_SESSION['myName']); ?>
    }
});

But the code is not working here. It is not unsetting the php session variable. Please provide me a solution. Thanks.

Note : I can do it by using AJAX by loading the php code from another page. But I want it to be done by using jQuery only.

Or is there any solution in jQuery that I can unset session variable without using PHP code?

1 Answers1

0

It will not work this way.

All PHP codes are executed before Javascript function . To achieve this :

  • You have to do AJAX or form submission.

You just cant do it by only jQuery.

Shifat
  • 732
  • 1
  • 6
  • 20