raspucin_22
raspucin_22

Reputation: 35

setting array session value inside jquery

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 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;

Upvotes: 1

Views: 3364

Answers (3)

Joshua - Pendo
Joshua - Pendo

Reputation: 4371

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.

Upvotes: 0

Felix
Felix

Reputation: 699

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

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

Upvotes: 1

jeroen
jeroen

Reputation: 91734

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.

Upvotes: 1

Related Questions