Ajax login form (PHP & Javascript)

Ok, here’s another AJAX login form example…
In this example I am using 3 javascript files, 2 php files and 1 stylesheet.

click http://joeabiraad.com/demos/ajax2 for the demo
click http://www.joeabiraad.com/demos/ajax2/ajax2.rar to download the files

Brief description:
_ajax.new.js: is the main ajax class. I have already explained this class
_formdata2querystring.js: used for posting the element of a form
_applogin.js: specific ajax class to handle the login form
index.php: simple login form
style.css: simple stylesheet

If you like it digg it
Next post will be a tutorial on how to make it
Stay tunned …


//Jo
Peace :)

Share and Enjoy:
  • Digg
  • description
  • Google
  • Facebook
  • del.icio.us
  • Mixx

134 Responses to “Ajax login form (PHP & Javascript)”


  1. 1 D. Peterson

    I’m trying download the source files, but every time I try I get a page with a bunch of nothing on it.

  2. 2 Arnulfo Arias

    Why this login form enables the back button?

  3. 3 Jo

    D.Peterson: Try to right click on the file then Save As.
    Its working fine here with me

  4. 4 Jo

    Arnulfo Arias: The login form will redirect you to welcome page when the login is correct.

  5. 5 Francesco

    To FARAWAY:

    The italian article talks about “Modern (Web 2.0?) ways of dealing with HTML Forms”. The article itself is a sort of link collection.

  6. 6 henkie

    it`s not working..

  7. 7 matej

    it doesn’t accept the password

  8. 8 Jo

    hi matej, can you please tell me what browser you are using ? and if you have javascript enabled or not.

    the password should be : iwannalogin ( no spaces and case sensitive )
    the email should be: email@gospam.biz

  9. 9 mister p

    kind of ugly that the password is being send in the post.
    you should encrypt the password before sending it with js to php and then decrypt it again with php.

  10. 10 Jo

    Yeah you are right,
    I will do it today and update the script… thank you mister p :)

  11. 11 jb

    When i submit i’ve always this message “going somewhere ?”
    i don’t know why please help me

  12. 12 Jo

    Hi jb
    in the file js/_ajaxlogin.js

    go to line 105, it should be:
    self.ajax.doPost(’ouf.php?action=login’, postData, self.handleLoginResp);

    In your case i think it is not

    check it and if u need anythg more reply :)

  13. 13 jb

    Thank’s but i’ve always the same problem

    go to
    http://notedeletudiant.com/ndl/test2/testlogin/

  14. 14 Jo

    Hi jb
    Im sorry for this error, you should add this line at the beginning of ouf.php ( before switch($action) ).
    ###
    $action=trim($HTTP_GET_VARS['action']);
    ###

    or re-download the files… i fixed them
    Let me know if that worked for you

  15. 15 jb

    perfect!! Thank you very much :)

  16. 16 TheMan

    It’s not working on IE, the submit button never gets activated… any ideas?

  17. 17 Jo

    TheMan: Can you please tell me what IE version you have ?
    I tested it on IE 6 and it seems to work.
    Make sure you do not copy and paste the password, because then it wont work.

    Or better yet, upload somewhere on the net so i can see it

  18. 18 TheMan

    You are right Jo, it does work, i have IE 6.0, mi mistake :)

  19. 19 max

    help for this error!

    self.form.Pass has no properties
    [Break on this error] self.form.Pass.value = ”;

  20. 20 mcnk

    doesnt work very well…

  21. 21 mcnk

    or something i dont understand…got the same error that max…text “welcome.html” appears in the box…
    can this be due to the fact i work on local server (easyphp) ?

  22. 22 Jo

    Hi mcnk
    Can you upload your script somewhere so i can see the error ?

  23. 23 Dj Tiesto

    Could you please help me with one thing ? I need the LOG-OUT thing. I made the login case at ouf.php to save the sessions, for keeping user on the page if he will try to refresh the page, but now i dont know how to create an AJAX Request for my ‘LOGOUT’ case, which i made and filled with session_unset(); session_destroy(); and etc. Could you please help me with this ? Best reguards, Tiesto )

  24. 24 Jo

    Hi Dj Tiesto
    All you have to do is add the javascript function in js/_applogin.js that will call your case at ouf.php
    the function can be smthg like this:
    this.a_logout(){
    var self = Login;
    self.ajax.doGet(’ouf.php?action=logout’, ‘text’, self.handleLogoutResp);
    }
    then implement the handleLogoutResp function
    this.handleLogoutResp = function(str) {
    var self = Login;
    var respArr = str.split(’,');
    var respType = respArr[0].toLowerCase();
    var respMsg = respArr[1];
    var respSes = respArr[2];
    if (respType == ’success’) {
    location = respMsg+’?'+respSes;
    }
    else {
    self.showErrorPrompt(respMsg);
    }
    }

    Finnally add this to the end of the _applogin.js

    document.getElementById(’logout_id’).onClick = Login.a_logout;

    where logout_id could be the id of ( Logout

    If you need anything else tell me.

    By the way can I see the website where u implemented my ajax function ? :)

  25. 25 Justin

    Is it possible to have multiple login email addresses and passwords?

  26. 26 Nik Potaper

    Google is my favorite search engine!

  27. 27 Fil

    I agree, multiple users and the ability to change where the user ends up after hitting submit depending on their username would be great!!!

  28. 28 Jo

    I will do second version of this login form and include the following:
    1- logout
    2- session/cookie for each multiple user
    3- ability to redirect ppl depending on the user to a specific page

  29. 29 Avinash

    Sir, I want to create a login form, that will differentiated between the Administrator and User with their Authorty

  30. 30 Avinash

    Sir, I aslo have a problem to develop a page of on line exam, So that the User can not make copy of this form

  31. 31 Avinash

    Please reply soonly

  32. 32 Sleepwalker

    It’s a good idea BUT…
    You got some wrong things in the code.

    1. you can type any email that you want
    2. you don’t have to write the whole email just a letter or two.. not even the @
    3. you can login how ever you want to.. you just ned the a correct password. It has nothing to do with the email.

  33. 33 sameh

    its cool but i would prefer if it didn’t go to another page

  34. 34 Mark Bowen

    Hi there,

    Just came across this and whilst it seems to work okay if you click the button, if you press return whilst in any of the fields it will let you in no matter what you type into the fields.

    Just thought you should know.

    Best wishes,

    Mark

  35. 35 Jo

    This login form does not register any session/cookie.
    You will have to this yourself

  36. 36 Wendy Primus

    Thanks for this login page…. it works ……

  37. 37 Lamonte

    Doesn’t want to work in IE 7

  38. 38 dgh

    ghdfgh

  39. 39 hone

    self.form.ChangeAlert.checked is null or has no properties

  40. 40 gunniz

    t’s a good idea BUT…
    You got some wrong things in the code.

    1. you can type any email that you want
    2. you don’t have to write the whole email just a letter or two.. not even the @
    3. you can login how ever you want to.. you just ned the a correct password. It has nothing to do with the email.

    ——-

    Just change:
    if ($password == $good_pass){

    To:
    if (($password == $good_pass) && ($email == $good_email)){

  41. 41 Kevin

    yeah , thats pretty cool!

  42. 42 Salik

    IT’S VERY NIC BUT I FOUND JS ERROR ON IE 7 IF U ENTER INCORRECT EMAIL AND PASSWORD

    BUT OVERALL IT’S VERY GOOD EXCELLENT JOB

    REGARDS
    SALIK AHMED

  43. 43 Dave

    When I put de User & pass text “welcome.html” appears in the box. I need to redirect another page or addresscom. Can u help me?

  44. 44 Salvador Franco

    hi, it’s no work
    same error going somewhere ?

    ouf.php ( before switch($action) ).
    ###
    $action=trim($HTTP_GET_VARS[’action’]);
    ### this line its ready
    i am not sure what happend can u help me please

  45. 45 sikh

    Wow!!!^

  46. 46 MeiK

    I uploaded all the files into the server it does not bring me to the welcome.html page. and i was still in the index.php page with the message display ‘ going somewhere ?’ but when i test it using localhost, it works. can you help me out with tis problem?

  47. 47 RB2004

    Hi, it wont load at all, in firefox it loads the message in the top box, but i get the error going somewhere? latest version used, in ie though, nothing, no messages nothing.. please help? Thanks

  48. 48 RB2004

    Also forgot to mention, running on IIS with php installed

  49. 49 Simug

    Hi everone,
    I have a question.
    Could you please help me ?
    How can a server understand when lots of users login simultaneously ?
    I am working with PHP but I cant write a script for this.
    thanks from now

  50. 50 Antoine

    Hi, Thats beautifullogin u implemented. I had to remake my login using ur code. but apparently the ajax part doesnt work. i have other works in ajax and they work fine. this is how it is now. wen i hit the submit button it starts loading the whole part no ajax working. and am using fire bug the error there is

    self.form has no properties
    http://127.0.0.1/_applogin.js?2122778914
    Line 76

    what could be there problem. its really frustrating me. thanx

  51. 51 mona

    Amazing really amazing script thank you

  52. 52 Jo

    Very Cool Script….

    I am waiting for your Version 2 : Login …

  53. 53 KB

    When I am downloading it, then ajax2.rar gets download but when I open it there is nothing in it.
    Please help me. and send me the files to my email
    thanx

  54. 54 AJAX Login System using XMLHttpRequest

    cool script

  55. 55 jem

    great form.
    but i dont understand how t use the javascript stuff.
    wont open on my pc.
    where do i put it in my site?

  56. 56 jeroen

    This is a nifty script ;) Well, it’s asked before. Do you know if it’s easy to use for a multi-login purpose. So integrate a variable which identifies the loginform.
    That should be really great!! Hopefully someone can help me with this.

    Thx!

  57. 57 WBGX

    Nice script, it was just what I was looking for, however, I noticed once you get to the welcome page you can bookmark it and return later bypassing the login. Is there a way to make someone always login?

  58. 58 jeff

    nice cript. but what you do when you get to the page . how can i protect the pages and the files in the in there , so users MUST login to see the pages ?

    thanks
    jeff

  59. 59 Eugene

    Hi… good contribution.
    Is there a way I can redirect to a url which has been typed in earlier, rather than to the welcome page.
    Many thanks.

  60. 60 okan

    Thanks. Thanks all.

  61. 61 HardwareKid

    Great script, using it on my new project :D

  62. 62 derya ajans

    thank you for share.

  63. 63 hudhta

    to Max
    change self.form.Pass to self.form.password

  64. 64 hudhta

    to hone
    self.form.ChangeAlert.checked is null or has no properties
    in _applogin change uncoment //self.enableScreenReaderFeatures();//Joe

  65. 65 Fox

    Hello!

    I wondered if you planned a review & update of that script. It’s wonderful that way, but some options which are probably easy for any experienced PHP/MySQL coder would be awesome…
    -Register form
    -MySQL for accounts storage
    -A mini admin panel allowing to add/delete accounts and to modify informations
    -Sessions: to remember when the user has logged-in.

    That’s all. Also, you promised us a tutorial and I haven’t seen it… is it still planned?

    Regards,
    Fox

    Also, maybe a bit more secured features…?

  66. 66 john

    Hi,
    i have this message:undefined, and i don’t know why. Help me please.
    Adress of website: http://jbv.ced.travel/connexion_utilisateur.php

  67. 67 ertarunz7

    hi,
    i need to refresh two / {whatever is possible} at one time by clicking a button but not refreshing the other fields of a form, nor by posting other fields , it should just work as the captcha refresh works, and refreshes the captcha code by interfering the rest of the fields. A help will be highly appreciated and thankful.

  68. 68 umesh

    hi,
    i am trying to use the script on one of my site i was trying without chaining any of the script but still i get the error
    “going somewhere ?”

    i have this line in code as mentioned above
    $action=trim($HTTP_GET_VARS['action']);
    and this below line also

    line numner 105, it is :
    self.ajax.doPost(’ouf.php?action=login’, postData, self.handleLoginResp);

    still i get the error can u help me please

  69. 69 ferry

    its working thx guys..

  70. 70 fırat çöloğlu

    thank you for share

  71. 71 M.Dubb

    How to “enable” the submit button “only” once all validation completes?
    If the validation is not complete, no submit.

    Is it possible and how to enable this function?

    Cheers

  72. 72 M.Dubb

    Forgot to add.
    I have used this to validate against the database by means of username, email address and would only like to allow the submit button to be available or enabled once the validation is happy:)

    Cheers

  73. 73 MG

    Wow, thanks a lot for this one.
    greetings from Amsterdam.

  74. 74 Hank

    If Your On A Mac It Works So Perfectly! THANK YOU!

  75. 75 müzik dinle

    Thank you very much mr.

  76. 76 Brad

    The login.php file in the current rar file seems to only be a partial file. The file ends with ‘<’.

  77. 77 Romo

    Hi dude its just displaying “going somewhere ?” :-S

  78. 78 Romo

    finally got it man… thnx for d nice idea

  79. 79 Ap

    great work, thanks

  80. 80 Eyad

    thank you for this script

  81. 81 priya

    hello,

    when i tried to run i got this error..

    Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0

    Fatal error: Unknown: Failed opening required ‘/var/www/apache2-default/ajax/login1/index.php’ (include_path=’.:/usr/share/php:/usr/share/pear’) in Unknown on line 0

    please help me soon

  82. 82 Ben

    This works beautifully…locally (XAMPP). However, on my host, it doesn’t, and returns the “going somewhere?” response. So for those with the “going somewhere?” error, it may be the way the Ajax is handled on your host.

  83. 83 Ben

    The solution to my “going somewhere?” error was that my host ignores $HTTP_GET_VARS. Changing that to $_GET resolved the issue.

  84. 84 Ronnie

    Hi,
    nice script!
    Is there going to be an update to it any time soon?

  85. 85 Farasy

    Many thanks for sharing.

  86. 86 Nikon

    I will do it today and update the script… thank you

  87. 87 Nicholas Patten

    Hey, awesome work.
    Is there any way to hide the password and login names in ouf.php, if someone wanted they could check the source of the index.php, then download the accompanying js files and figure out it’s routed to ouf.php and then they download ouf.php and then whala, login and password. Is there a way to remove this capability?

    I like what you’ve done, taught me quit a lot so far. :)

  88. 88 Himel Khan

    its working exactly.
    Thanks…………

  89. 89 ARTI

    Thanks you very much all ready again.

  90. 90 Ritesh

    Fantastic script Jo. Had to tweak a few codes to make it work for us, but it works wonderfully.

  91. 91 sucks?

    this thing isn’t even available anymore? no demo no zip file? k

  92. 92 Deviantz

    very nice scripts thanks for sharing this. keep it up!

  93. 93 Shoola

    let someone send the code to my email: waleshoola@yahoo.com. thanks

  94. 94 Jo
  95. 95 Marie Santos

    Can you please provide more details on this?

  96. 96 Andrew

    Hi Joe !
    Can you post an example of AJAX contact form ?

    Thanks !

  97. 97 Alexwebmaster

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  98. 98 mario oyunları

    thanks for sharing

  99. 99 zesteeraw

    Was ist das?

  100. 100 baalaji

    how to access database values in ouf.php
    i tried to compare user name and password from database
    its not working

  101. 101 d

    lopll;;

  102. 102 d

    hi

  103. 103 sdfsdfsdf

    thanx

  104. 104 Joe

    I’m just downloaded the zip file and threw it on the wamp server as is and when the index page fist launches, i get a “less then” operator in the lower left hand corner which indicate to me that there is something off with the code. But I went ahead to try to log in with the hint you provide but nothing registrars in the box. Anyone else experience this? Also i see in the index page there’s 2 js code that have ?” withing the call but should it have <?php in that call?

  105. 105 W

    Can it work on IE8?

  106. 106 wow power leveling

    Great article, again. These informations are especially useful …

  107. 107 jaychen

    how to make it? please…

  108. 108 Alexander

    Hey there!
    So far, your script is working really well for me.
    I am having a huge problem that is driving me up the wall, though.

    When submitting my form, the link in which I would like to be redirected to is shown. My guess is it’s a problem with the server on handling everything, but I really need the redirect to work. I’ve tried other ways around it, but since the responseFormat and whatnot are all set to text/XML/object and I can’t seem to get it to anything with HTML- I can’t get a redirect at all. Please please please help me out as quickly as possible!

    -Alexander

  109. 109 elizer

    nice tutorial.. thx

  1. 1 Italian translator needed :P - FARAWAY
  2. 2 napyfab:blog» Blog Archive » links for 2007-09-26
  3. 3 PortalMods.com » Blog Archive » Ajax login form (PHP)
  4. 4 Doontime - Freelance web designer » Blog Archive » Top 100 AJAX form scripts for 2007
  5. 5 Webmaster 38 » Blog Archive » Ajax login form (PHP) at ajax scripts compound
  6. 6 Top 100 AJAX 'form' related scripts for 2007 | Nobox Media
  7. 7 41 of the Best MooTools Ajax Example Downloads | Speckyboy - Wordpress and Design
  8. 8 KMC | Web & Internet Teknolojileri Günlüğü » En iyi 41 mooTools & Ajax Örneği
  9. 9 Mayron Cimardi Blog » Lista: Links legais (e úteis!) de Ajax/DHTML
  10. 10 Ajaxified: 7 Ways to AJAX up your site! | David's Blog
  11. 11 AJAX Login Form Script » » pixey.de
  12. 12 Most Wanted Ajax Techniques: 50+ Ajax Examples and Tutorials | Noupe
  13. 13 Most Wanted Ajax Techniques: 50+ Ajax Examples and Tutorials | SulVision
  14. 14 Ajaxian » Groups of 50+ Ajax Examples
  15. 15 Most Wanted Ajax Techniques « Program2.0
  16. 16 +50 Ejemplos en Ajax | ProyectoAurora.com
  17. 17