This topic is locked

How To Access Javascript Variable

1/28/2013 10:06:46 AM
PHPRunner General questions
D
dannche author

Dear All,
Is there a way to access JavaScript variable from PHP? I know the help document has How to Access PHP variables but none for access JavaScript Variable.
In my Before Display events, I assigned the following:

$pageObject->setProxyValue("AlumniCount", $_SESSION["AlumniCount"]);

$pageObject->setProxyValue("GroupCount", $_SESSION["GroupCount"]);
Then in my JavaSript On Load Event I have the following code:

proxy.AlumniCount = --proxy.AlumniCount;

alert(proxy.AlumniCount);

$_SESSION['AlumniCount'] = proxy.AlumniCount;}
The Alert shows the value of proxy.AlumniCount decrease by one. So I know it works. But when I try to assign proxy.AlumniCount back to my session variable ($_SESSION['AlumniCount']) it does not work.
Appreciate any help on how to resolve this.
Thanks in advance.
Regards,

Danny.

C
cgphp 1/28/2013

You can't assign a value to a PHP session variable (server side code) on the client side. You need an ajax call to update the session var. Check the jquery ajax method: http://api.jquery.com/jQuery.ajax/

D
dannche author 1/28/2013



You can't assign a value to a PHP session variable (server side code) on the client side. You need an ajax call to update the session var. Check the jquery ajax method: http://api.jquery.com/jQuery.ajax/


Dear Cristian,
Can you help to give me some pointers? I check the website you stated and I have no clue where to begin. Really appreciate your help. Many thanks.
Regards,

Danny

Admin 1/28/2013

Simply add the following to your BeforeDisplay event:

$_SESSION["AlumniCount"] = $_SESSION["AlumniCount"]-1;
D
dannche author 1/28/2013



Simply add the following to your BeforeDisplay event:

$_SESSION["AlumniCount"] = $_SESSION["AlumniCount"]-1;



It won't work if I do as you suggest because I need to increase or decrease the AlumniCounter based on the change event of an object. Example:
ctrlAlumniID.on('change', function(e){

if (this.getValue()== '3')

{

ctrlAlumniConference.show();

ctrlAlumniConference.addValidation("IsRequired");

proxy.AlumniCount = --proxy.AlumniCount;

}
Regards,

Danny

Admin 1/28/2013

I see, it's in fact more complicated. You still need to move this logic to the server side code, that's where you can manipulate session variables.
Assuming this is an Add page you can use something like this in BeforeAdd event:

if ($values["AlumniID"]==3)

{

$_SESSION["AlumniCount"] = $_SESSION["AlumniCount"]-1;

}