1

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;
2
  • 1
    You do understand the difference between scripts that run on the server side and those that run on the client side? Moreover, where do you get $IDGame from? Is it sent to the server using a query parameter? Do you check its validity? Commented May 5, 2011 at 23:28
  • please provide the code snippet of the part where you are retrieving the values. Commented May 5, 2011 at 23:29

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@Marcel Korpel That's true, also for a normal link, but the set session array in jquery which I call inside of javascript function seems to imply staying on the same page, but I could be wrong...
Yeah, but it sounds to me more like he doesn't get the difference between server and client scripts.
I am already making ajax request using jquery. I just need to add element to the php session array variable. I don't know how to do that
@jeroen I am staying on same page(no reloading page)
@bob0800 That's what I assumed as well. Can you provide more code, the javascript and complete php of the page you are calling with ajax?
1

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

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

3 Comments

Is there such a $.isArray function in PHP?
I don't use PHP, the question said inside JQuery, so I was taking a stab at a javascript answer where JQuery was available. I take it $_Session is a PHP thing?
Read the others' comments. My "answer" was going under the assumption this was all in javascript and you had your own associative array called $_SESSION. From what I can tell they're right and you're mixing your PHP and javascript code. Oh and PENDO wasn't saying you had to use hidden fields, just that his example made one up with the id = IDGame, for illustration purposes only. You could use jQuery.data, or shudders make a global called IDGame or whatever. The hidden input wasn't the point it was the cookie plugin.
0

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.

2 Comments

I don't wanna use hidden fields, because I would like to do it using session varibles
see Felix' reply above.. the hidden field is just meant to supply the value to the jQuery. I think the right answer is being given here a couple of times already and you should be fine. You should add some more code for us to see where the problem really is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.