Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Wednesday, January 18, 2012

Build a login form for your mobile app with DHTMLX Touch

Build a login form for your  mobile app with DHTMLX Touch

In this introduction to open source JavaScript framework DHTMLX Touch it

explains how you can implement a login form for a mobile web app and send form values to the server with Ajax

DHTMLX Touch is an open source JavaScript framework for building mobile web apps. Providing necessary API and ready-to-use UI controls, DHTMLX Touch helps developers to create responsive HTML5 interfaces for touch devices. As an example we're going to create a web login form for mobile.
A login form is an essential component of modern websites and applications. Since the web is now moving to mobile devices, it’s important to have all parts of the web interface touch-ready and looking good on small screens.

This tutorial describes the basic steps for implementing a user login form for a mobile web app built with DHTMLX Touch.

In addition to other goodies, DHTMLX Touch already includes a form control that's perfect for our task. We will go through the complete process of creating and designing the form, sending form values to the server with Ajax, and processing the response.

The above picture shows the final look of the login form we’re going to build. Continue reading and you'll see that the layout and appearance of the form can be easily changed to meet your design requirements.

With the variety of server side technologies, you can choose which one to use on the backend but we’ll omit this part and focus only on the client-side implementation involving HTML, CSS, and JavaScript. If you’re not familiar with the methods of user authorisation yet, you can find related articles on the web.

Getting started

To start, we need to create a new HTML5 document and include the DHTMLX Touch library in it. Download the latest version of DHTMLX Touch. The .zip package includes all the necessary files and demos. Extract the content and copy all the files from the ‘codebase’ folder in the project directory.

On the development stage it’s better to use the debug version of the files as it helps you catch possible errors and get some additional info. When the page is ready, you can replace the debug version with the files that contain compressed code to reduce the weight of the library.
  1. <!DOCTYPE HTML>
  2. <html>
  3.     <head>
  4.         <title>DHX Touch: Login Form</title>
  5.         <script src="libs/touchui_debug.js" type="text/javascript"></script>
  6.         <link rel="STYLESHEET" type="text/css" href="libs/touchui_debug.css">
  7.     </head>
  8.     <body>
  9.     </body>
  10. </html>
Don’t forget to specify the doctype. It’s needed so the browser reads this document as HTML5. When the DHTMLX Touch library is attached to the page, we can start creating a login form.

Building a form

First we’ll create a form in default skin, and then define form layout, styling, and other features ilater. As a standard login form, our form will include four elements:

Two text fields:
  • Login field
  • Password field
Two buttons that are located in the same line:
  • Login button
  • Register button
Let’s create a form layout.

DHMLX Touch uses the “view” term for the UI components and elements. There are five views in our layout which include “form” and its four fields, text fields and buttons. Each view is described by a JSON object. And all objects of the layout need to be gathered in one, depending on the views hierarchy.

Our “form” view will contain the “elements” property with the array of form fields. However, views in elements collection are laid out vertically, one below another. For this reason, we use "cols" array to locate two buttons in the same row (in columns).

Here is the layout:
  1. <script>
  2.     var form = {
  3.             view:"form", elements:[
  4.             { view:"text", label:"Login", name:"login", labelWidth:90 },
  5.             { view:"text", type:"password", label:"Password", name:"password", labelWidth:90 },
  6.             {
  7.                 type:"clean",
  8.                 cols:[
  9.                     { view:"button", type:"form", label:"Login" },
  10.                     { view:"button", type:"round", label:"Register" }
  11.                 ]
  12.             }
  13.         ]
  14.     };
  15. </script>
To create the form we call the dhx.ui() method with this object.
  1. <script>
  2.      dhx.ready(function(){
  3.         dhx.ui.fullScreen();
  4.         dhx.ui(form);
  5.      });
  6. </script>
Note that we put our code within the dhx.ready() method. This approach allows us to execute the code when page sizes are set (similar to window.onload). We also call fullScreen(), which allows the page and its components to be displayed in the full-screen mode.

Once we’ve got the form with the default styling (see picture below), we can move on and enhance the form with the proper layout and styling.


Defining form layout

The DHTMLX Touch framework provides a flexible way to position UI elements on a page. Each element can be defined as a row in "rows" collection or a column in "cols". To build complex structures, you can put rows into a column or columns into a row. This approach allows developers to lay out elements any way they like. We will use "rows" and "cols" to position the form elements to build a login view.

Looking at the picture of the final form it’s not hard to guess what elements can be described by rows and columns. We can separate four rows and three columns in the third row:
  • The first row is a toolbar with title
  • The second row is just empty space
  • The third row includes three columns:
    o    empty column on the left (15px width)
    o    the form itself
    o    empty column on the right (15px width)
  • The forth row is an empty space, which is two times higher than the second row

Now we’re building this form using DHTMLX Touch:
  1. <script>
  2.     var form = {
  3.         /*created in step 2*/
  4.     };
  5.     /*toolbar*/
  6.     var toolbar = {
  7.         view:"toolbar", elements:[
  8.                 { view:"label", label:"Member Area", align:"center" }
  9.         ]
  10.     };
  11.     var loginView = {
  12.         type: "clean",
  13.         css: "layout",
  14.         rows:[
  15.             toolbar,
  16.             { gravity:1 },
  17.             {
  18.                 type:"clean",
  19.                 cols:[
  20.                     { width:15 },
  21.                     form,
  22.                     { width:15 }
  23.                 ]
  24.             },
  25.             { gravity:2 }
  26.         ]
  27.     };
  28.     dhx.ready(function(){
  29.         dhx.ui.fullScreen();
  30.         dhx.ui(loginView);
  31.     });
  32. </script>
We’ll get a login form with the default styling and the layout we’ve defined:

The next step will be applying a new style and adding custom design to the login form.

Form styling

To make our form look like the final screenshot we need to do the following:
  • Change toolbar styles
  • Set overall background image for all views (elements)
  • Set form styles
We will start with the toolbar styles. To apply a new CSS class for a view, we need to set the class name in the CSS property. Let's set it for the toolbar:
  1. <script>
  2.     var toolbar = {
  3.          view:"toolbar", css:"toolbar", elements:[
  4.             { view:"label", label:"Member Area", align:"center" }
  5.         ]
  6.     };
  7. </script>
The code which defines styles for the toolbar with labels is the following:
    1.  /*toolbar*/
    2.     .toolbar{
    3.         background-image:url(../images/toolbar.png);
    4.         background-repeat:repeat-x;
    5.     }
    6.     /*label in toolbar*/
    7.     .toolbar .dhx_el_label div{
    8.         font-family:Gergia;
    9.         font-size:30px;
    10.         color:#636363;
    11.         text-shadow: 0 1px 1px #d9d9d8;
    12.     }
    This is how the toolbar on the top should look now:

    To set one background image for all elements, we’ll set it for rows collection and make the other views transparent:
    1. <style>
    2.     ...
    3.     /*sets the overall background*/
    4.     .overall{
    5.         background: url(../images/bg.png);
    6.     }
    7.     /*makes all views inside overall container transparent*/
    8.     .overall .dhx_view{
    9.         background-color:transparent;
    10.     }
    11. </style>
    12. <script>
    13.     ...
    14.     var loginView = {
    15.         css: "overall",
    16.         rows:[
    17.             ...
    18.         ]
    19.     };
    20.     ...
    21. </script>
    Both backgrounds for toolbar and a web page should now appear like this:

    Next, we change the style of the login form: set background, border radius and shadows. We’re adding this code on the page to define the appearance of the form:
    1. <style>
    2.     .form{
    3.         background-image:url(../images/form.png);
    4.         -moz-border-radius: 10px;
    5.         -webkit-border-radius: 10px;
    6.         border-radius: 10px;
    7.         -moz-box-shadow: inset 0 -1px 2px 2px #444141;
    8.         -webkit-box-shadow: inset 0 0 2px 2px #444141;
    9.         box-shadow: inset 0 -1px 2px 2px #444141;
    10.     }
    11.     ...
    12. </style>
    13. <script>
    14.     var form = {
    15.         id:"loginForm", css:'form', view:"form", height:135, elements:[
    16.             ...
    17.         ]
    18.     };
    19. </script>
    We're almost done. Move on and change the design of input fields:
    1. /*input outer container*/
    2. .dhx_el_box{
    3.     background:#ffffff !important;
    4.     -moz-box-shadow: 0px 1px  #d2d2d, inset 0px 2px 2px #a6a6a6;
    5.     -webkit-box-shadow: 0px 1px #d2d2d2, inset  0px 2px 2px #747981;
    6.     box-shadow: 0px 1px #d2d2d2, inset 0px 2px 2px #747981;
    7.     border-top:0px !important;
    8. }
    9. /*text inputs*/
    10. .dhx_inp_text{
    11.     border-radius: 0px;
    12.      -webkit-appearance: none;
    13.     background:transparent;
    14. }
    Finally, we add the required style to our form buttons. We need to set a border radius for both buttons and set a new background colour for the button "Register".
    1. /*Login (type:"form") and Register (type:"round" buttons)*/
    2. .dhx_el_formbutton input, .dhx_el_roundbutton input{
    3.     -moz-border-radius:5px;
    4.     -weblit-border-radius:5px;
    5.     border-radius: 5px;
    6.     -moz-box-shadow: none;
    7.     -webkit-box-shadow:  none;
    8.     box-shadow:  none;
    9. }
    10. /*Register button*/
    11. .dhx_el_roundbutton input{
    12.     background: -webkit-gradient(linear, left top, left bottom, from( #a0b4ce ),to( #5e6e80 ));
    13.     background: -moz-linear-gradient(top,  #a0b4ce,  #5e6e80 );
    14.     border-color:#5a697b #5a697b #243242;
    15. }
    16. /*Register button, touched state*/
    17. .dhx_touch .dhx_el_roundbutton input{
    18.     background: #8496ad;
    19. }
    After these modifications, we get the design of the login form exactly the way we defined it in the beginning of the tutorial.


    Sending Ajax request and processing the response

    Once we've created a form that looks and feels native on iPhone and Android, we can now learn how to send the form data using Ajax and show the app's welcome page after a successful login.
    We'll make the Ajax call on "Login" button click. We define the function that will be called in the "click" property of the "Login" button:
    1. <script>
    2.    var form = {
    3.         id:"loginForm", css:'form', view:"form",height:135, elements:[
    4.             { view:"text", label:"Login", name:"login", labelWidth:90 },
    5.             { view:"text", type:"password", label:"Password", name:"password", labelWidth:90 },
    6.             {
    7.                 type:"clean",
    8.                 cols:[
    9.                     { view:"button", type:"form", label:"Login", click:"login();" },
    10.                     { view:"button", type:"round", label:"Register" }
    11.                 ]
    12.             }
    13.         ]
    14.     };
    15. </script>
    And here is the login() function that we've just set:
    1. <script>
    2.     function login(){
    3.         dhx.notice({ delay:750, message:"Checking ..."});
    4.         var formData = $$('loginForm').getValues();
    5.         dhx.ajax().post("login.php", formData, afterCall);
    6.     }
    7. </script>
    Here is the explanation of the above function line by line:
    1. The first line shows "Checking ..." pop-up message within 750 ms
    2. With the second line, we get form values
    3. And the last line sends the form data in "POST" Ajax request to server-side script
    The first parameter of the dhx.ajax().post() method is a string containing the URL to which the request is sent (the server-side script which will process the login request).

    afterCall parameter is the function that will be called after the response is received. And if the response is JSON data: { result:'success'} or { result:'fail'}, this function can look like:
    1. <script>
    2.     function afterCall(text, xml){
    3.         var data = dhx.DataDriver.json.toObject(text,xml);
    4.         if (data.result == "success"){
    5.             dhx.alert("You have been succesfully logged in!");
    6.         } else{
    7.             dhx.alert("The login and password are incorrect!");
    8.         }
    9.     }
    10. </script>
    In this function, dhx.DataDriver.json.toObject creates a JSON object from the response data. Then it validates the "result" of the object and shows the corresponding alert.

    When a user is logged in, they should be directed to a new “Welcome” page. To add this new page, we need to modify the structure of our app and add a new view. For example, we'll show an image as a welcome screen:
    1. <script>
    2.     var loggedIn =  {
    3.         id:"loggedIn",
    4.         template:'<img src="../images/logged_in.png">'
    5.     };
    6.     var app = {
    7.         cells:[
    8.            loginView,
    9.            loggedIn
    10.         ]
    11.     }
    12.     dhx.ready(function(){
    13.         dhx.ui.fullScreen();
    14.         dhx.ui(app);
    15.      });
    16. </script>
    To show the view from "cells" collection, we need to call show() method for it. We add it to afterCall function:
    1. <script>
    2.     function afterCall(text, xml){
    3.         var data = dhx.DataDriver.json.toObject(text,xml);
    4.         if (data.result == "success"){
    5.             /*shows "loggedIn" view*/
    6.             $$("loggedIn").show();
    7.         } else{
    8.             dhx.alert("The login and password are incorrect!");
    9.         }
    10.     }
    11. </script>
    That's it! Our login form is ready and I hope you’ve learned how to create a custom login form for your next mobile web app or site! You can do a lot more with DHTMLX Touch, and it’s a framework that's fairly easy to learn. If you want to get some new experience in web development for mobile devices, just give it a try.

    No comments:

    Post a Comment