0

Hello friends!

I'm just beginning with JavaScript (and Ajax).

My loginscript now works with Ajax - that is; the script is sending a request to the servers PHP script to check username and password, but I wonder what that script should return - and how to use that response.

Either XML or pure text: The response is now "false" if the user couldn't log in for some reason, and the users ID and real name if the login went well...

But then the problem: if login IS ok - so that the users ID and real name is returned from the serverscript - I would like to store those variables in a session variable ($_SESSION in PHP). How do I get those variables back to the client script from JavaScript?

Hope someone understand the problem.

Best regard

Kjell Inge Sandvik
Norway

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by ryan_vietnow
0

Kjelle,

I understand most of what you ask but this paragraph is slightly confused:

But then the problem: if login IS ok - so that the users ID and real name is returned from the serverscript - I would like to store those variables in a session variable ($_SESSION in PHP). How do I get those variables back to the client script from JavaScript?

First: Session variables are entirely server-side, so you simply set $_SESSION etc. (Your question implies passing them to the client then back to the server again in order to store them).

Second: Client script IS JavaScript. I wonder, do you mean "How do I get those variables back to the client script from PHP?"

To make PHP variables available to javascript in an AJAX response, the simplest approach is to use something called Javascript Object Notation (JSON). PHP and javascript both support JSON allowing you to json_encode server-side and json_decode client-side.

Server-side: Simply use PHP's (now) native json_encode().

Client-side: Whereas json is native in some browsers, you are best off using a json lib or a more general lib which includes json support, (eg jQuery).

Airshow

Edited by Airshow: n/a

0

My goal is to use the server php-script to find out if that user exists (registered). If so, the script should return the users ID and real name back to the javascript.

At the same time, a session-variable should be set so that the user is actually logged in through the application.

I manage to get the data from the serverscript to the client. The problem is: how to store that data in session-varables? Can I just store them in the servers PHP-script as "usual" and just forget passing them back to the client?

0

Of course you will create your session variables at the serverside script(your process script) then you can query on your view page if that session variable exists or not(or whatever you want from it).

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.