Get ready for BLACK FRIDAY shopping starting in

Forums

PHP

This topic is locked

Get php session

Posted 07 Sep 2007 19:56:26
1
has voted
07 Sep 2007 19:56:26 Devorah Fleisher posted:
How can I get a php session variable into a javascript variable.
ie:
productID = "<?php $_SESSION['productID'] ?>"
when I try to see what the variable contains with
alert (productID);

I do not get any data.

What did I do wrong?

Replies

Replied 13 Sep 2007 14:56:28
13 Sep 2007 14:56:28 Georgi Kralev replied:
Hi Devorah,

Make sure that you have start up the session and your session variable has value.
For example the following code work without any problems on my computer:
<i>
&lt;?php
<b>session_start(); </b>
<b>$_SESSION['data'] = 1;</b> // store session data
?&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
&lt;/head&gt;
&lt;SCRIPT LANGUAGE="JavaScript"&gt;
var productID = "&lt;?php <b>echo $_SESSION['data'];</b> //retrieve data ?&gt;";
alert (productID);
&lt;/SCRIPT&gt;
&lt;body&gt;
Display Session data with JavaScript
&lt;/body&gt;
&lt;/html&gt;</i>

I hope this helps.

Regards,

Georgi Kralev

----------------------------------
Support - www.DMXzone.com

Reply to this topic