1. Code
  2. JavaScript
  3. jQuery

jQuery Mobile Framework - A Forms Tutorial

Scroll to top

jQuery Mobile is a relatively new mobile web framework, with its first release made in October 2010. The framework has many interesting features to support development of web based mobile applications. In this tutorial, we will focus on some basic elements of jQuery Mobile: page structure, forms and Ajax form submission. The tutorial is based on version 1.0 alpha release 2 of the jQuery Mobile framework.

As part of the tutorial we will write a small B2C application. A package shipment company has a form in their web site for customers to enter information on packages that are lost or damaged during shipment. Using a hand-held device (e.g. a web enabled phone) a customer inputs information to the form about their claim: the shipment number from the original receipt, name/address, and a description of loss/damage. When the customer submits the form, the web site responds with a claim id for further tracking. The scenario is shown in the diagram below:

Tutorial application context diagramTutorial application context diagramTutorial application context diagram

Figure 1. Tutorial application context diagram.

jQuery Mobile framework supports a variety of browsers including iOS devices, Android devices, Blackberry OS 6, and webOS (for a support matrix, see http://jquerymobile.com/gbs/). The application in this tutorial has been tested against an Android 2.2 device and an iOS 4.1 device.

Design Considerations

Before going into technical details, let us talk about main considerations driving the design of the application.

  • Short response time: It is preferred for the web application to contain a single html page with a simple user interface. All user interface constructs, such as error dialog and confirmation pages, will be part of the html page. Once the page is downloaded into the browser, different sections of the page will be shown to the user depending on the particular context. There will be no need to open multiple network connections to download pages several times.
  • Error handling: If the user forgets to enter a required field in the form, submission should fail with a warning dialog. Missing fields should be easy for the user to find.
  • Support for multiple devices/browsers: Applications should have consistent look-and-feel and behavior across supported devices and browsers.

Of course, a typical real-life application will have additional or different design concerns. For the purposes of this tutorial, our scope will be limited to the considerations above.

jQuery Mobile Introduction

Most of the discussion in this section has been borrowed from the documentation in http://jquerymobile.com. See the original reference for further details.

jQuery Mobile is a user interface system built on the popular JavaScript framework jQuery. It consists of user interface elements and programming constructs that provide consistent functionality across a large variety of mobile and desktop web browsers. The 'Progressive Enhancement' and 'Graceful Degradation' principles are behind its design. Core functionality of the framework supports a broad set of platforms whereas newer platforms benefit from more enhanced features.

jQuery Mobile has a relatively small footprint. Since basic configuration of a jQuery Mobile page is done purely by markup, it is possible to achieve quick development cycles, particularly if your application does not need complex programming functions. Although built on jQuery core, theming system of jQuery Mobile is based on a new CSS framework that aims to separate color and texture from structural styles that define things like padding and dimensions. An event handling API handles touch, mouse, and cursor focus-based user input methods in a unified manner.

jQuery Mobile comes out of the box with many user interface elements such as header and footer toolbars, buttons with icons, form elements (including touch sensitive slider and toggle switches) and lists. Basic HTML styles, two or three column grids, and collapsible elements are also provided.

Inclusion of jQuery Mobile Libraries

As of jQuery Mobile 1.0 alpha 2 release, the following stylesheet and JavaScript libraries must be included in your HTML pages. You can reference them as below or serve them from your own server:

1
2
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.css" />
3
<script src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
4
<script src="http://code.jquery.com/mobile/1.0a2/jquery.mobile-1.0a2.min.js"></script>

Container and Content Pages

Let us introduce the basic structure of a page in jQuery Mobile. A page in jQuery Mobile framework can be a single page or a local internal linked 'page' within a page. A 'container' page will include one or more 'content' pages. The boundary of a container page is defined as follows:

1
2
<div data-role="page">
3
...
4
</div>

Observe that the value of the data-role attribute is page. On the other hand, the boundary of a content page is defined as follows:

1
2
<div data-role="content">
3
...
4
</div>

Observe that the value of the data-role attribute is content.

A content page may have an associated header and footer. As an example, a container page with several content pages may have the following structure:

1
2
<div data-role="page">
3
 
4
  <div data-role="header">...</div> 
5
  <div id="contentWithHeaderAndFooter1" data-role="content">...</div> 
6
  <div data-role="footer">...</div>
7
	
8
  <div data-role="header">...</div>
9
  <div id="contentWithHeaderAndFooter2" data-role="content">...</div> 
10
  <div data-role="footer">...</div>
11
	
12
  <div id="contentWithNoHeaderAndFooter" data-role="content">...</div> 
13
  
14
</div>

When a container page is loaded, all content pages in it will be visible. In our application, we need to display only one content at a time. Therefore, we need to programmatically control which content will be displayed depending on context.

Hiding/Showing Content Pages

In order to hide an element, use the jQuery Mobile API hide() function:

1
2
$('#hdrMain').hide();

will hide the header with id hdrMain. Here, we used the jQuery ID selector by passing id of the element we would like to select preceded by # sign. Conversely, the show() function shows a hidden element:

1
2
$('#hdrMain').show();

The hide() and show() functions apply to elements of many different types, in particular, <div> tags and anchors (<a> tags). In this tutorial, we will use hide() and show() functions extensively to display only the relevant context to the application user. More details are given below.


Step 1: Demo Application Page Structure

Our demo application consists of a single html page, index.html, which consists of a container page as shown below:

1
2
<div data-role="page" data-theme="b" id="page1">
3
4
  <!-- ====== main content starts here ===== -->
5
  <div data-role="header" id="hdrMain" name="hdrMain" data-nobackbtn="true">...</div>
6
  <div data-role="content" id="contentMain" name="contentMain">
7
    ...
8
  </div>
9
  <div data-role="footer" id="ftrMain" name="ftrMain"></div>
10
  <!-- ====== main content ends here ===== -->
11
  
12
  <!-- ====== dialog content starts here ===== -->  
13
  <div align="CENTER" data-role="content" id="contentDialog" name="contentDialog">
14
    ...
15
  </div>
16
  <!-- ====== dialog content ends here ===== -->  
17
  
18
  <!-- ====== transition content page starts here ===== --> 
19
  <div data-role="content" id="contentTransition" name="contentTransition">
20
    ...
21
  </div>
22
  <!-- ====== transition content ends here ===== --> 
23
  
24
  <!-- ====== confirmation content starts here ===== --> 
25
  <div data-role="header"  id="hdrConfirmation" name="hdrConfirmation" data-nobackbtn="true">...</div>
26
  <div data-role="content" id="contentConfirmation" name="contentConfirmation" align="center">
27
    ...
28
  </div>
29
  <div data-role="footer" id="ftrConfirmation" name="ftrConfirmation"></div>   
30
  <!-- ====== confirmation content ends here ===== --> 
31
    ...
32
</div><!-- page1 -->
  • The main content contains the form to be filled out by the user and has both a header and a footer.
  • Dialog content is displayed only if a required form field is missing when form is submitted. It consists of a warning and an OK button to close the dialog. Observe that it does not have a header or footer.
  • Transition content is displayed after the form is submitted until the response is received from the server. It consists of a "spinner" image with short text informing the user that their form is being submitted. Transition content also does not have a header or footer.
  • Confirmation content is displayed a after response is received from the server. It displays the confirmation number to the user. Confirmation content has both a header and a footer.

Content transitions are shown in the diagram below:

Content transitionsContent transitionsContent transitions

Figure 2. Content transitions.

Additional observations on the code listing above:

  • The data-theme attribute allows us to choose from available styles in the jQuery Mobile framework: <div data-role="page" data-theme="b" id="page1">. The default theme has various color swatches named a, b, c, d, e, each providing a consistent set of colors for different elements in the page. For our application we chose the color corresponding to b.
  • Headers come with a back button. We did not need back buttons and therefore chose to hide them via data-nobackbtn="true".
  • It is possible to provide some text to be displayed in the footer between the <div> tags. In our application we omitted text in the footer. As a result, the user will see in the footer only a thin bar colored the same as the header. With text, the footer will have a similar height as the footer with the text in it.

Form Elements

Our claims form consists of the following fields:

  • Several input fields of type text: Shipping number, First name, Last name, Email, Phone, Street address, City and Postal code. All are required fields except Phone.
  • A select element for State. This is a required field.
  • A textarea element for a user to enter information regarding the missing or damaged shipment. This is a required field.

As an example, let us look at the text field for Shipping number:

1
2
<div id="shipDiv" data-role="fieldcontain">
3
  <label for="shipNo">Shipping number*</label>	
4
  <input id="shipNo" name="shipNo_r" type="text" />
5
</div>

We used a label with a for attribute whose value is the same as the id of the input element the label is attached to. Furthermore, we wrapped the label and input in a div with data-role attribute valued as fieldcontain. jQuery Mobile framework will utilize the fieldcontain attribute value for special styling. In addition, look at name="shipNo_r". For this particular application, we decided to define the value of the name attribute of any required form element to be the value of the id attribute appended by _r. If the element is not required, the value of the name attribute will be the same as the value of the id attribute. For example, since Phone is not a required field, it is defined as follows:

1
2
<div id="phoneDiv" data-role="fieldcontain">
3
  <label for="phone">Phone</label>		
4
  <input id="phone" name="phone" type="text"/>
5
</div>

As we will see later on, this special naming convention will help us detect missing fields during form submission.

Another notable form element is the select element. Similar to the above, the value of the for attribute is the same as the id of the select element the label is attached to. Also, the label and select are wrapped in a div with the data-role attribute value as the fieldcontain. With the long list of options that we have in this application, jQuery Mobile framework will open the list in a new page when the user focuses on the select element.

1
2
  <div id="stateDiv" data-role="fieldcontain">
3
    <label id="stateLabel" for="state">State*</label>		
4
    <select id="state" name="state_r" tabindex="2">
5
      <option value="ZZ">Please select a state</option>
6
      <option value="AL">Alabama</option>
7
      <option value="AK">Alaska</option>
8
      <option value="AZ">Arizona</option>
9
      ...
10
    </select>
11
  </div>

The form page as displayed in an Android 2.2 phone is shown below. The user will scroll through the page to access the elements in the form:

Form pageForm pageForm page

Figure 3. Form page in an Android phone.

The same form is shown in an iPod touch running iOS 4.1:

Form pageForm pageForm page

Figure 4. Form page on an iPod touch.

Step 2: Variable Definitions

We will reference various elements in the html page throughout our code. It makes sense to define those references only once and reuse them as needed. For this reason, we declare globally used variables as well as constants in the head section of the page:

1
 
2
<script>	 
3
  // Global declarations - assignments made in $(document).ready() below

4
  var hdrMainvar = null;
5
  var contentMainVar = null;
6
  var ftrMainVar = null;
7
  var contentTransitionVar = null;
8
  var stateLabelVar = null;
9
  var whatLabelVar = null;
10
  var stateVar = null;
11
  var whatVar = null;
12
  var form1var = null;
13
  var confirmationVar = null;
14
  var contentDialogVar = null;
15
  var hdrConfirmationVar = null; 
16
  var contentConfirmationVar = null;
17
  var ftrConfirmationVar = null;
18
  var inputMapVar = null;
19
  
20
  // Constants

21
  var MISSING = "missing";
22
  var EMPTY = "";
23
  var NO_STATE = "ZZ";
24
</script>

Assignments of those variables are done in jQuery $(document).ready() function using ID selectors, as shown below. (Recall that jQuery $(document).ready() function is called when the entire DOM hierarchy in the page is loaded. That function is the best location to initialize our variables.) We also define inputMapVar as a collection consisting of all input elements with _r in their name attribute (The function call $('input[name*="_r"]') selects all such elements).

1
2
<script>	 
3
  $(document).ready(function() {
4
    // Assign global variables

5
    hdrMainVar = $('#hdrMain');
6
    contentMainVar = $('#contentMain');
7
    ftrMainVar = $('#ftrMain');
8
    contentTransitionVar = $('#contentTransition');
9
    stateLabelVar = $('#stateLabel');
10
    whatLabelVar = $('#whatLabel');
11
    stateVar = $('#state');
12
    whatVar = $('#what');
13
    form1Var = $('#form1');
14
    confirmationVar = $('#confirmation');
15
    contentDialogVar = $('#contentDialog');
16
    hdrConfirmationVar = $('#hdrConfirmation');
17
    contentConfirmationVar = $('#contentConfirmation');
18
    ftrConfirmationVar = $('#ftrConfirmation'); 
19
    inputMapVar = $('input[name*="_r"]');
20
    ...
21
  });
22
  ...
23
</script>

Step 3: Content Selection Functions

Let us now look at the content selection functions that will be called by event handlers.

For hiding and displaying the Main content and its header/footer, we use the hideMain() and showMain() functions:

1
2
function hideMain(){
3
  hdrMainVar.hide();
4
  contentMainVar.hide();
5
  ftrMainVar.hide();   
6
}
7
8
function showMain(){
9
  hdrMainVar.show();
10
  contentMainVar.show();
11
  ftrMainVar.show();
12
}

For hiding and displaying the transition content, we use the hideContentTransition() and showContentTransition() functions:

1
   
2
function hideContentTransition(){
3
  contentTransitionVar.hide();
4
}      
5
6
function showContentTransition(){
7
  contentTransitionVar.show();
8
}

For hiding and displaying the Dialog content, we use the hideContentDialog() and showContentDialog() functions:

1
   
2
function hideContentDialog(){
3
  contentDialogVar.hide();
4
}   
5
6
function showContentDialog(){
7
  contentDialogVar.show();
8
}

Finally, for hiding and displaying the confirmation content and its header/footer, we use the hideConfirmation() and showConfirmation() functions:

1
   
2
function hideConfirmation(){
3
  hdrConfirmationVar.hide();
4
  contentConfirmationVar.hide();
5
  ftrConfirmationVar.hide();
6
}  
7
8
function showConfirmation(){
9
  hdrConfirmationVar.show();
10
  contentConfirmationVar.show();
11
  ftrConfirmationVar.show();
12
}

When the page is loaded, only the main content should be displayed. For this reason, other content pages are hidden:

1
2
<script>	 
3
  $(document).ready(function() {
4
    // Assign global variables     

5
    ...
6
    hideContentDialog();
7
    hideContentTransition();
8
    hideConfirmation(); 
9
  });
10
  ...
11
</script>

We will discuss how to tie together those content selection functions with the event handlers below.


Step 4: Form Submission

When a user presses the submit button we need to validate the form data before actually submitting the form. To keep things simple, we will only check if all the required fields have been provided. If form validation is successful, we display the transition content which consists of a spinner image with a short text informing the user that their form is being submitted. If validation fails, we display the dialog content which consists of a warning and an OK button to close the dialog.

Form Validation

Labels of the form elements with missing data will be highlighted in red. For this purpose, we add a new style class named missing and extending the jQuery Mobile label class.

1
2
label.missing  {
3
  color:#FF0000;
4
  font-weight:bold;
5
}

Below is the event handler for form submission. In typical jQuery notation, the identifier for form1 is passed to the jQuery object call to handle the submit event:

1
2
$('#form1').submit(function() {
3
  var err = false;
4
  // Hide the Main content

5
  hideMain();
6
  
7
  // Reset the previously highlighted form elements

8
  stateLabelVar.removeClass(MISSING); 
9
  whatLabelVar.removeClass(MISSING);              
10
  inputMapVar.each(function(index){              
11
    $(this).prev().removeClass(MISSING); 
12
  });
13
  
14
  // Perform form validation

15
  inputMapVar.each(function(index){  
16
    if($(this).val()==null || $(this).val()==EMPTY){            
17
      $(this).prev().addClass(MISSING);            
18
      err = true;
19
    }          
20
  });   
21
  if(stateVar.val()==NO_STATE){           
22
    stateLabelVar.addClass(MISSING);                     
23
    err = true;
24
  }            
25
  if(whatVar.val()==null||whatVar.val()==EMPTY){   
26
    whatLabelVar.addClass(MISSING);   
27
    err = true;
28
  }
29
  
30
  // If validation fails, show Dialog content

31
  if(err == true){            
32
    showContentDialog();
33
    return false;
34
  }        
35
36
  // If validation passes, show Transition content

37
  showContentTransition();
38
39
  // Submit the form

40
  $.post("/forms/requestProcessor.php", form1Var.serialize(), function(data){
41
    confirmationVar.text(data);
42
    hideContentTransition();
43
    showConfirmation();
44
  });        
45
  return false;      
46
});

We set an error flag to false and hide the main content that contains the form. We then reset the previously highlighted labels on each member of the collection inputMapVar. To do that, we pass an inline function as an argument to each() that simply contains $(this).prev().removeClass(MISSING);. Note that this is the selected input element and prev() returns its immediate previous sibling which is the label associated with it.

The state for state selection and what for claim description are not text fields. Therefore, the corresponding labels have their styles reset separately.

After all the previously highlighted labels are reset we revisit the required input elements to check if any of them has a missing value. If that is the case we add the missing class to the label associated with the input field:

1
2
  // Perform form validation

3
  inputMapVar.each(function(index){  
4
    if($(this).val()==null || $(this).val()==EMPTY){            
5
      $(this).prev().addClass(MISSING);            
6
      err = true;
7
    }          
8
  });   
9
  if(stateVar.val()==NO_STATE){           
10
    stateLabelVar.addClass(MISSING);                     
11
    err = true;
12
  }            
13
  if(whatVar.val()==null||whatVar.val()==EMPTY){   
14
    whatLabelVar.addClass(MISSING);   
15
    err = true;
16
  }

In addition, the error flag is set to true and the error dialog is shown. The figure below shows the error dialog in our Android 2.2 phone:

Error dialogError dialogError dialog

Figure 5. Error dialog.

After the user presses the OK button, the user is displayed the form page with the missing fields highlighted, as shown below. In that picture, the orientation of phone screen is horizontal. Observe that each label and its sibling text field are displayed in a single line as opposed to the vertical orientation in Figure 3 where the label is above the text field.

Form pageForm pageForm page

Figure 6. Form page with a missing field highlighted.

On the other hand, if validation is successful we show the transition content and submit the form as discussed below.

Submitting the Form via Ajax

The form submission uses the jQuery Mobile post function that accepts three arguments:

1
2
$.post("/forms/requestProcessor.php", form1Var.serialize(), function(data){...});
  • The first argument is the server URL to submit the form to.
  • The second one is the form content to submit. To get the form content, we simply call serialize() on the jQuery object by passing it the identifier for our form.
  • The third argument is an in-line function to process the response, data, sent back from the server.

Note that the post function performs an Ajax call which is asynchronous by nature. Immediately after calling the post, program execution will continue by returning false from the submit function and user will start seeing the Transition content.

TransitionTransitionTransition

Figure 7. Transition page displayed during form processing.

The in-line function to process the response is executed only when the server returns its response. For simplicity, the server application processing the form data, requestProcessor.php, returns a hard-coded claim id for the user to use for future reference. Before sending the claim id requestProcessor.php, it sleeps 4 seconds to emulate server processing time. It is in this period that the application will display the Transition content.

1
2
<?php
3
usleep(4000000);
4
5
echo('FTREIK12345678');
6
?>

When the server response is received, the event handler code is executed. The first step is to populate the span tag named confirmation with the value returned from the server. This is simply done with:

1
2
confirmationVar.text(data);

Then, we hide the Transition content and show the Confirmation content that contains the span tag named confirmation:

1
2
<div data-role="content" id="contentConfirmation" name="contentConfirmation" align="center">	
3
    <p>A new claim has been created based on data you have submitted.</p>
4
    <p>Your confirmation number is: <span id="confirmation" name="confirmation"></span>  </p>
5
</div>

The confirmation page displayed in our Android 2.2 phone is shown below (phone's orientation is horizontal):

ConfirmationConfirmationConfirmation

Figure 8. Confirmation page in Android 2.2.

The same confirmation page is displayed on an iPod touch as follows (orientation is vertical):

Confirmation

Figure 9. Confirmation page in iPod touch.

Deploying the Source Code

The source code for the tutorial has a simple folder structure. All resources are stored in a folder named forms. In that folder, there are two sub-folders, css and img. The css folder contains colors.css, which has the label.missing class, and img stores wait.gif, the spinner image. The index.html and requestProcessor.php files are directly under the forms folder. In our testing, we used an Apache web server with version 2.2.11 running PHP version 5.3.0. If you place the forms folder directly under the DocumentRoot of the web server, you can access the application via http://[your_host_name]/folder/index.html.


Conclusion

In this tutorial we introduced basic concepts from jQuery Mobile framework with focus on page structure, basic styling, form elements and Ajax form submission. A sample claims processing application was introduced to demonstrate those concepts. We provided screenshots of various pages of the application executing in an Android 2.2 phone and an iPod touch device with iOS 4.1. Some observations and closing remarks are given below:

  • Since jQuery Mobile is built on jQuery core, those who have prior knowledge of the jQuery framework could get up to speed with the jQuery Mobile framework easily.
  • The ability of the framework to represent multiple content pages in a single html page with built-in functions to show/hide those content pages appeared very convenient to create a form application with different views depending on state. For example, this technique can be applied to implement 'wizard' style user guides or multi-page survey forms.
  • Tested on desktop, Android 2.2, and iOS 4.1 platforms, both the look-and-feel and functional aspects of the sample application were consistent. During development, it should be possible to quickly test and debug the main aspects of a jQuery Mobile application in a desktop platform. Then, more testing can be done on the individual devices and browsers that the application is expected to support.
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.