11 Replies - 9427 Views - Last Post: 14 July 2011 - 01:45 AM Rate Topic: -----

#1 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 10:56 AM

Hey,

I'm using an auto complete form, using a php file to get the data, this works fine with a hard-coded ID, but I need to set this ID dynamically using a session variable.

This is the call to the PHP script, which returns the data.
<script type="text/javascript">
        $(document).ready(function() {
            $("#to").tokenInput("Dat/autocomplete.php", {
                theme: "autocomp"
            });
        });
</script>



I'm unsure as to how to pass the session variable through to the PHP file. I tried using preg replace on ?id={user_id} and using GET in the php file, but to no avail :(

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Session Variable Through JQuery Call

#2 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 11:14 AM

Hi, just assign the session variable value to a hidden input then use
 $("#my_hidden_input").val();



Hope that helps.
Was This Post Helpful? 2
  • +
  • -

#3 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 11:26 AM

Hey,

Am I right in thinking this would be right:
        $(document).ready(function() {
            $("#to").tokenInput("Dat/autocomplete.php",
			$("#user_id").val(),
			{
                theme: "autocomp"
            });
        });



Also, how would I pick up the value within the PHP file?

This post has been edited by E_Geek: 13 July 2011 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 12:13 PM

I have no experience with the plug-in you are using but if you are working with hard coded values and it is fine couldn't you just do the same for the variable value?

Also, is there a particular reason you are passing your session value through Javascript? Why not just access the value in the $_SESSION array in autocomplete.php ?
Was This Post Helpful? 1
  • +
  • -

#5 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 12:28 PM

When I said hard-coded I meant within autocomplete.php, should of been clearer sorry.

For some reason $_SESSION won't work within autocomplete.php, and I have no idea why.
Was This Post Helpful? 0
  • +
  • -

#6 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 12:35 PM

It's strange that you can't access sessions in your autocomplete file. It sounds like it would be an ideal solution if your autocomplete uses session values. I would suggest trying to run down why that happens.

As far as the Javascript goes, like I said I am not familiar with that plug-in so I can't help much there. Sorry, wish I could be of more help.
Was This Post Helpful? 1
  • +
  • -

#7 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 946
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 12:36 PM

Don't try to pass the session id around like that. It's actually very dangerous and is the only thing that protects a user's session from being stolen. Why? Each session has in id that is used to determine what variables ($_SESSION)or resources on the server belong to them.

Another reason is you don't need to pass the session id back to the server because the server already knows it.

What are you trying to achieve by passing the session id?
Was This Post Helpful? 2
  • +
  • -

#8 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 01:16 PM

I'm trying to access a session variable I have set from within the code, but when I try to access it directly it won't let me, I keep getting NULL value, although the session variable is definitely set.
Was This Post Helpful? 0
  • +
  • -

#9 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 01:20 PM

Are you sure you have session_start() at the top of your autocomplete page? What do you get if you do:
print_r($_SESSION);


?
Was This Post Helpful? 2
  • +
  • -

#10 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 01:33 PM

autocomplete.php isn't ran directly, its through a series of includes.

index.php starts the session, which in turn includes the file that calls autocomplete.php through the javascript I posted earlier. The only part of autocomplete.php I see through my script is the autocomplete form itself, which works if I set id=x within autocomplete, but not if I use id=$_SESSION['id'].

I'm calling session_start() in autocomplete.php itself and it's working fine now, thanks :D

Am I right in thinking that the JS call isn't actually including the file, which is why I needed to call session_start() directly in autocomplete.php ?

This post has been edited by E_Geek: 13 July 2011 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

#11 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Passing Session Variable Through JQuery Call

Posted 13 July 2011 - 03:05 PM

Each script needs to have session_start() somewhere in it if you want to use sessions. This can be in the form of a direct call or from including a file that uses it. Either way it has to be there to access the $_SESSION array.

Edit: Yes the AJAX call does not behave the same as including on the server side. Think of the server side file as it's own entity.

This post has been edited by Jstall: 13 July 2011 - 03:07 PM

Was This Post Helpful? 1
  • +
  • -

#12 E_Geek  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 45
  • View blog
  • Posts: 236
  • Joined: 20-February 11

Re: Passing Session Variable Through JQuery Call

Posted 14 July 2011 - 01:45 AM

Thanks for all the help and for clearing that up :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1