-1

This is my jQuery code for taking id value when I click itemslist

$(document).ready(function() { 
  $('.itemslist ul li').click(function()
    { $id = $(this).attr("class"); 
   }); 
});

But session is not taking that id value

session_start(); 

$_SESSION['catname'] = $id;

$id= $_SESSION['catname']; 

session_destroy(); 

Can any one please say the answer about this?

4 Answers4

5

You can't assign values to PHP variables from withing Javascript/Jquery. You should make an AJAX call to a PHP file that handles the creation/destroying of the SESSION.

See this answer.

Community
  • 1
  • 1
Kenny
  • 5,350
  • 7
  • 29
  • 43
0

you can use ajax for send your id to server page and store your id in session.

  $(document).ready(function() {    
$('.itemslist ul li').click(function()
        { id = $(this).attr("class"); 
$.ajax({
        type   : "POST",
        url    : Postpage.php,
        data   :"id="+id ,
        success: function (json) {
            alert(json); 
          }

});  
});

Server side

session_start(); 
$id=$_REQUEST['id'];
$_SESSION['catname'] = $id;

$id= $_SESSION['catname']; 

session_destroy(); 
Man Programmer
  • 5,300
  • 2
  • 21
  • 21
0

for that you need to call the Ajax to store the session value, given example code here

$(document).ready(function() { 
   $('.itemslist ul li').click(function()  { 

  var id = $(this).attr("class");
    $.ajax({
                type   : "POST",
                url    : youphppage.php,
                data   :"id="+id ,
                success: function (msg) {
                    alert(msg); // you will get stored session value here       
                  }
      });
   });
 });

In your php page while ajax call,

session_start(); 

$_SESSION['catname'] = $_POST['id'];;

echo $id= $_SESSION['catname']; 
iLaYa ツ
  • 3,941
  • 3
  • 32
  • 48
0

Use jquery ajax to send the data in a php file in post method and retrieve the value on that page

   $(document).ready(function() { 
      $('.itemslist ul li').click(function()
        { var a = $(this).attr("class");
          var dataString = 'a=' + a; 
        $.ajax(function(){
          type : "POST",
          url  : "session.php",
          data : dataString,
          cache: false,
          success: function(data){
              alert('session sent!');
         }
       });
  }); 

});

and the session.php:

session_start(); 

$a= issest($_POST['a']):$_POST['a']:"";

$_SESSION['catname'] = $a; 

session_destroy();
Kaidul
  • 15,409
  • 15
  • 81
  • 150