开发者

setting array session value inside jquery

开发者 https://www.devze.com 2023-03-03 00:08 出处:网络
I am trying to set session array in jquery which I call inside of javascript function that is called onClick event for link.

I am trying to set session array in jquery which I call inside of javascript function that is called onClick event for link.

But it keeps setting me my last choice that I click.

This is开发者_StackOverflow社区 code I used for setting session array(I wanted to add new element to session array everytime when someone clicks on the link):

$_SESSION['Ticket'][]=$IDGame;


You are mixing up server-side and client-side languages. If you want to add something to your $_SESSION variable (server-side), you will need to make an ajax request in javascript (client-side) to the server.


I think this is what you're getting at....

$.isArray($_SESSION['Ticket']) ? $_SESSION['Ticket'].push($IDGame) : $_SESSION['Ticket'] = [$IDGame];


You cannot use PHP code within jQuery (not in this case at least). There is a plugin for jQuery (http://plugins.jquery.com/files/jquery.cookie.js.txt) based on the parameters that are given you can setup a cookie or a session for the current user. For instance:

$('#element').click(function(e) {
  e.preventDefault();
  $.cookie('Ticket[]', $('#IDGame').val();
});

This code assumed the $IDGame is stored in a (hidden) textfield with ID = IDGame. This is the proper way using jQuery with sessions and cookies. If you want to use PHP Code per sé, than you should consider loading a PHP file with the getJSON function and sending the ID as a parameter to the file and adding a new key to the session in the background.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号