How to make a modal AJAX login box with Mootools
Saturday, October 13, 2007 5:49:48 PM
It's basically what you can do in JavaScript with alert() but with different functionality: If you do alert('foo'), you get a box with text "foo" that you must close before doing anything else. This kind of dialogs are called modal dialogs.
In this post I'll show you how to make a modal login box that can work nicely even in browsers without proper JavaScript support. This is quite easy with the proper tools.
For this article, you will just need to know some HTML and JavaScript.
For this script, I decided to use Mootools. Mootools is a lightweight JavaScript library, similar to Scriptaculous and Prototype. The reason I chose Mootools over my usual tool Scriptaculous is that I wanted to experiment with other libraries as well. We could use Mootools itself to create our dialog box, but we can make it even easier for us by using MOOdalbox. MOOdalbox is a script for making nice modal dialog boxes and it's based on Mootools.
Start!
First, you need to download MOOdalbox. The archive comes with both MOOdalbox and Mootools, so you won't need to download them separately. Just unzip the files to where you will be putting the rest of your files related to this. Next, let's make a simple HTML page with a link to our login page, and the login form.Laying out the HTML
We need some page to act as the "portal" to our login area, so let's make a simple page with a link to it. We need to remember ot include the MOOdalbox scripts and styles to it, too.<html> <head> <title>MOOdalbox example</title> <link rel="stylesheet" type="text/css" href="css/moodalbox.css" /> <script type="text/javascript" src="js/mootools.js"></script> <script type="text/javascript" src="js/moodalbox.js"></script> </head> <body> This page will link to the login page<br /> <a href="login.html">Log in</a> </body> </html>This does not use any JavaScript yet, but we will get to that part soon. Now, let's create the login box code.
<form action="login.php" method="post"> Login <input type="text" name="login" /><br /> Pass <input type="password" name="pass" /><br /> <input type="submit" value="Log in" /> </form>And save that as login.html. We won't add the html and other tags to this one because it will be included with AJAX to the main page, but if it's like this, we can also use it with a browser that does not have JavaScript. Now you have a login function on your page. I won't go into details of setting up the actual log in procedure, just how to make the modal box.
Adding the magic
So how do we go about making our login box appear like an alert box? Simple! We add a rel attribute to the link, like this:<a href="login.html" rel="moodalbox">Log in</a>If you click your Log in link now, you get a nice modal dialog which displays login.html. Easy, huh?







Theo HubenetPimpyT # Sunday, October 14, 2007 6:03:23 PM
Janizomg # Sunday, October 14, 2007 8:27:34 PM
Anonymous # Friday, November 2, 2007 7:33:55 AM
Janizomg # Friday, November 2, 2007 10:54:40 AM
What comes to the security code, I don't think it's useless.
Whoever would want to bypass the code would have to know that they need to look for the span element. I don't think all spambots contain code to "look for a span, it contains the security code"
And no it's not mine. It's something which is shown automatically by My Opera if you're an anonymous commenter.
Anonymous # Thursday, November 8, 2007 5:56:48 PM
Razvan Bratesr4zv4n # Monday, November 19, 2007 9:35:22 PM
I've mentioned your tutorial on the official MOOdalBox blog to bring more people to it.
More people should do what you did - i.e. write simple, bitesize tutorials, as a jumpoff point to a better knowledge of web design / development.
In the meantime, I'll set up a demo page on my web server and post back with a link
Anonymous # Friday, November 23, 2007 4:57:06 PM
Janizomg # Saturday, November 24, 2007 4:23:29 AM
Anonymous # Friday, January 11, 2008 5:50:01 PM
Anonymous # Sunday, January 13, 2008 11:06:12 AM
Anonymous # Friday, April 4, 2008 10:01:18 PM
Janizomg # Saturday, April 5, 2008 11:55:08 AM
Anonymous # Tuesday, July 22, 2008 9:19:29 PM
Victor Alberto Gilvhanla # Monday, July 28, 2008 10:49:55 PM
Anonymous # Monday, August 11, 2008 7:47:43 PM
Anonymous # Thursday, October 2, 2008 11:41:27 PM
Anonymous # Saturday, October 4, 2008 7:35:05 PM
Anonymous # Tuesday, October 7, 2008 11:08:41 PM
Anonymous # Sunday, November 16, 2008 4:51:36 PM
Anonymous # Monday, March 16, 2009 11:59:58 AM
Anonymous # Monday, March 16, 2009 12:00:41 PM
Anonymous # Monday, March 16, 2009 12:30:30 PM
Anonymous # Monday, April 13, 2009 1:35:06 AM
Anonymous # Thursday, April 30, 2009 9:08:00 AM
Anonymous # Thursday, April 30, 2009 9:08:44 AM
Anonymous # Thursday, September 17, 2009 12:01:06 PM
Anonymous # Wednesday, June 23, 2010 4:06:20 PM