Friday, June 02, 2006

More Ajax!

The login, logout, and signup code can now be accessed by javascript. They're located at /php/login.php, /php/logout.php, and /php/signup.php.

Javascript code is returned from the php page as a string, via the responseText method on an xmlHTTPRequest. Since it's just a string at that point it must be evaluated via the javascript eval(string) function. This will create the variables, which then be used as any variables would.

Login.php will return two javascript variables: success, which can be true or false, and message, which will be either "You are now logged in." or "Bad username or password." If success=true, this means the user has been successfully logged in and the first message is given. Otherwise, the user has not been logged in and the second message is given.

Logout.php returns the same two variables as login.php, but the values should always be true and "You are now logged out." If something goes horribly wrong and the user is not logged out, it wouldn't get to the point of returning those variables anyway.

Signup.php returns the success variable as before, plus an array called "errors". The first entry in the array (index 0) corresponds to the first field in the form, the second to the second, etc. Each entry in the array contains an error message. If there is no error message in an array index, it will be a blank string (i.e. "").

A demonstration of how these things can be called can be found here:
login
logout
signup
Please note that these pages are only for demonstration of the ajax calls and how to turn the returned strings into useful javascript, not an actual suggestion of how things should look/work.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home