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?