Monthly Archives: October 2010

Scrolling Image in jQuery

In this article, we will learn to display an image at the center of the browser window (enclosed in an invisible window). On clicking the image, it will scroll to the left (inside the enclosed window) and disappears. When the image totally disappears, it will again appear from the right side of the window and stops from where it started

HTML code for  “Scrolling Image in jQuery”

We need to write the HTML code to display an image. We also need to enclose the image element within a div element as shown below:

<body>

<div>

<img src=”image1.jpg” width=150px height=150px class=”image”/>

</div>

</body>

In the HTML code, we can see that the img element is enclosed within a div element which is assigned the id: ‘scroller’. The reason of making use of the div element is to assign the width and height to  the invisible window for the image (within which we want it to scroll).

Style Sheet code for  “Scrolling Image in jQuery”

We write the id selector  ‘#scroller’ in the style sheet so that the style properties defined in it can be automatically applied to the div element of id:’scroller’ without using jQuery code. The style sheet will also contain the class selector ‘.image’ to assign ‘relative’ property to ‘position’ property to the img element that is  very necessary to make the image scroll.

The style sheet: style.css may appear as shown below:

style.css

#scroller{

width:155px;

height:155px;

margin:auto;

overflow:hidden;

position:relative;

}

.image{

position:relative;

}

We can see that the id selector ‘#scroller’ contains the width and height property set to value ’155px’ to define the width and height of the invisible window for the image. The margin property is set to value ‘auto’ so that window takes the margin space from the browser window automatically making the window appear at the center of the width of the browser window. The value of the overflow property is set to hidden to make the area of the image that has scrolled out of the boundary of the window to become invisible. The position property is assigned the value ‘relative’ to make the image scroll relative to the enclosed window

The class selector ‘.image’ contains the position property set to value ‘relative’ to make the image scroll from its current position

jQuery code for  “Scrolling Image in jQuery”

The jQuery code to make the image scroll is as shown below:

$(document).ready(function() {

$(‘.image’).click(function (event){

$(this).animate(

{‘left’: -160}, ‘slow’,

function(){

$(this).css(‘left’,150);

$(this).animate({‘left’: 0}, ‘slow’);

}

);

});

});

We can see in above jQuery code that a click event is attached to the image. In the event handling function of the click event, animate method is used that makes the image to scroll to the left slowly (in the boundary of the window in which the image is enclosed) and stops at the distance of  -160px from the left side of the window i.e. making the image completely disappear. In the callback function of the animate method (which is invoked when the animate method is over – i.e. when the image is completely invisible), we use .css() method to make the image appear at the distance of 150px from the left side of the invisible window making a small portion of the left edge of the image to appear. In the same callback function, we again make use of the animate method to make the image scroll to the left and stop at the distance of 0px (from the left side of the invisible window) i.e. the image will stop from where it started

Initially, the image will initially appear as shown in Figure 1

Figure 1.  Image on loading the web page

On clicking the image, it will scroll to the left and will become invisible slowly as shown in Figure 2.

Figure 2.  Image scrolling to the left

When the image completely disappears, it appears again from the right side of the window and scrolls towards left as shown in Figure 3.

Figure 3.  Image appearing from the right border of the invisible window

More Articles :

Selecting Multiple options in Select element using jQuery

Serializing form data using jQuery

For more information, refer my book : “jQuery Recipes A Problem-Solution Approach” available at : http://www.amazon.com/jQuery-Recipes-Problem-Solution-Approach-Development/dp/1430227095/ref=sr_1_1?ie=UTF8&s=books&qid=1287320549&sr=8-1

Creating a Contextual Menu in jQuery

In this article, we will learn to display a menu with a few menu items in it. When any menu item is hovered (mouse pointer is moved over it), the menu item will be highlighted and information related to it will be displayed. When the menu item is clicked, user will be navigated to the related web site.

HTML code for “Creating a Contextual Menu in jQuery”

Let us make an HTML file to represent the menu heading: Books along with three menu items. We create menu and its three menu items with the help of two unordered lists, one nested inside the other. The list items contain the anchor elements to represent menu items and refer to the target web site: http://example.com where user is supposed to be navigated on selecting any menu item – it is a hypothetical web site. Also, we will write information about the three menu items in three paragraphs. The HTML file appears as shown below:

<body>

<table>

<td>

<ul>

<li><a href=”http://example.com”>Books</a>

<ul>

<li><a href=”http://example.com”>Web Development</a></li>

<li><a href=”http://example.com” id=”pgmng”>Programming</a></li>

<li><a href=”http://example.com” id=”datab”>RDBMS</a></li>

</ul>

</li>

</ul>

</td>

<td valign=”top”>

<p class=”web” >The wide range of books that includes how Web development can be done

with ASP.NET, PHP, JSP etc.</p>

<p class=”prog” >The wide range of books that includes developing Programming skills

in C, C++, Java etc.</p>

<p class=”rdbms” >The wide range of books that includes how Data Base Management is

done via Oracle, MySQL, SQL Server etc.</p>

</td>

</table>

</body>

To make menus to appear on left side and contents on the right side, we make a table and place the menu in the first column and the paragraphs containing information of the related menu items in the second column of the table

To give the appearance of a menu to the unordered list element, we need to apply certain styles to all the three elements: <u>, <li> and <a>. We write their type selectors in the style sheet file so that the properties in it can be automatically applied to these three elements. The style sheet file may appear as shown below:

Style Sheet for “Creating a Contextual Menu in jQuery”

style.css

ul {

width: 200px;

}

ul li ul {

list-style-type:none;

margin: 5;

width: 200px;

}

a {

display:block;

border-bottom: 1px solid #fff;

text-decoration: none;

background: #00f;

color: #fff;

padding: 0.5em;

}

li {

display:inline;

}

.hover {

background: #000;

}

In the style sheet file, the type selector ‘ul’ contains the width property set to 200px to define the width of the menu heading: Books. The type selector ul li ul will be applied to the menu items. It contains the list-style-type property set to value ‘none’ to remove the traditional bullets from the unordered list elements. The margin property is set to value 5 to make the menu items appear bit indented as compared to the menu heading. The width property is set to 200px to define the width of the menu item to accommodate. The type selector ‘a’ contains the display property set to value ‘block to make the anchor element to appear as a block and not as individual elements. The border-bottom property is set to value ‘1px solid #fff’ to make a solid white border of thickness 1 px to appear below every anchor elements (to act as a separator). The text-decoration property is set to value: ‘none’ to remove the traditional underline that usually appears below hyperlinks. The background color is set to blue and the foreground color is set to white for all the anchor elements. The padding property is set to .5em (i.e. 50% of the default font size) to define the spacing between the anchor text and its border

The type selector ‘li’ is set to value inline to remove any white space between the list items.

The CSS class ‘.hover’ contains the background property to set the background color of the menu item (anchor element) to black color when user clicks on it.

jQuery code for “Creating a Contextual Menu in jQuery”

The jQuery code to display information of the hovered menu item is as shown below:

$(document).ready(function() {

$(‘.web’).hide();

$(‘.prog’).hide();

$(‘.rdbms’).hide();

$(‘#webd’).hover(function(event){

$(‘.web’).show();

$(‘.prog’).hide();

$(‘.rdbms’).hide();

$(‘#webd’).addClass(‘hover’);

}, function(){

$(‘#webd’).removeClass(‘hover’);

});

$(‘#pgmng’).hover(function(event){

$(‘.web’).hide();

$(‘.prog’).show();

$(‘.rdbms’).hide();

$(‘#pgmng’).addClass(‘hover’);

}, function(){

$(‘#pgmng’).removeClass(‘hover’);

});

$(‘#datab’).hover(function(event){

$(‘.web’).hide();

$(‘.prog’).hide();

$(‘.rdbms’).show();

$(‘#datab’).addClass(‘hover’);

}, function(){

$(‘#datab’).removeClass(‘hover’);

});

});

Initially, we hide the information stored in all the three paragraphs (of the respective menu items). That is, we hide the information stored in the three paragraph of class: ‘web’, ‘prog’ and ‘rdbms’ as we will display them only when the related menu item is hovered.

We then attach a hover event to the first menu item: ‘Web Development’ i.e. to the anchor element of id: ‘webd’. In the first event handling function of the hover event (that is executed when this menu items is hovered), we set the paragraph of class ‘web’ (that contains the information of the Web Development) to visible mode displaying information related to the books of web development to the user. We keep the rest of the paragraph elements hidden. That is, the paragraph elements of class ‘prog’ and ‘rdbms’ will be kept hidden. Also we apply the properties defined in the style rule: ‘.hover’ (defined in the style sheet) to the hovered menu item to highlight it and remove the ‘hover style rule in the second event handling function of the hover event that is executed when mouse pointer is moved away from the menu item.

We attach a hover event to the second menu item: ‘Programming’ i.e. to the anchor element of id: ‘pgmng’. In the first event handling function of the hover event (that is executed when this menu items is hovered), we set the paragraph of class ‘prog’ (that contains the information of the programming subject) to visible mode displaying information related to the books of programming to the user. We keep the rest of the paragraph elements hidden. That is, the paragraph elements of class ‘web’ and ‘rdbms’ will be kept hidden. Also we apply the properties defined in the style rule: ‘.hover’ (defined in the style sheet) to the hovered menu item to highlight it and remove the ‘hover style rule in the second event handling function of the hover event that is executed when mouse pointer is moved away from the menu item.

Finally, we attach a hover event to the third menu item: ‘RDBMS’ i.e. to the anchor element of id: ‘datab’. In the first event handling function of the hover event (that is executed when this menu items is hovered), we set the paragraph of class ‘rdbms’ (that contains the information of the database subject) to visible mode displaying information related to the books of RDBMS to the user. We keep the rest of the paragraph elements hidden. That is, the paragraph elements of class ‘web’ and ‘prog’ will be kept hidden. Also we apply the properties defined in the style rule: ‘.hover’ (defined in the style sheet) to the hovered menu item to highlight it and remove the ‘hover style rule in the second event handling function of the hover event that is executed when mouse pointer is moved away from the menu item.

On execution of the above jQuery code, we will get a menu along with three menu items in it. When we hover any menu item that menu item will be highlighted and the information related to it will be displayed as shown in below given figure

Menu item remains highlighted on being hovered and related information also been displayed

More Articles :

Validating Date Through Jquery

How to Validate an Email Address Through Jquery

For more information, refer my book: “jQuery Recipes A Problem-Solution Approach” available at: http://www.amazon.com/jQuery-Recipes-Problem-Solution-Approach-Development/dp/1430227095/ref=sr_1_1?ie=UTF8&s=books&qid=1287320549&sr=8-1

Working with Forms

Forms are indispensible for acquiring user feedback or information. A form consists of input fields—text, checkboxes, radio buttons, and so on. The data entered in these fields is passed on to a specified script for further processing after the Submit button has been clicked. The script can be any appropriate language, such as PHP, ASP.NET, or Python. In this book, we’ll be using PHP as the server-side scripting language.

Here’s an example of a form:

<form action=”createuser.php” method=”POST”>

<ul class=”rounded”>

<li><input type=”text” placeholder=”Userid” /></li>

<li><input type=”text” placeholder=”Contact No”/></li>

<li><input type=”text” placeholder=”Email Address”/> </li>

</ul>

<a href=”#”>Submit</a>

</form>

Input text fields are assigned unique names—Userid, ContactNo, and EmailAddress—because data entered in the input fields is accessed by name only.

The attribute action points to a PHP file called createuser.php. The attribute method is set to POST, which means that the data entered into the input fields will be submitted to the createuser.php script using the HTTP request method POST for further processing. Form contents can be submitted to the specified script using either the GET or POST HTTP request methods.

  • GET—This method of passing form information is considered less secure than POST, as the data is actually displayed in the browser’s address bar, passed along with the URL of the script. For example, let’s assume that the PHP script to which we want to pass form’s information is createuser.php, and the names assigned to the input fields are Userid, ContactNo, and EmailAddress. When the Submit button is taped, the browser’s address bar would be:

crreateuser.php? Userid=John123&ContactNo=123456789&EmailAddress=johny123@gmail.com

Moreover, there is a limit on the amount of information passed through the GET method.

  • POST—This method is usually preferred because the information it is not displayed in the browser’s address bar and can handle larger amounts of data.

Note: The information sent by a form through GET and POST methods is stored in  the associative arrays $_GET and $_POST respectively.

Collecting Information Sent by a Form

Recall that the two HTTP request methods by which information of a form can be passed to a target PHP script are GET and POST. We also noted that the information is then stored in the associative arrays, $_GET and $_POST. The target PHP uses these associative arrays to retrieve the form information, so let’s talk a bit about them.

$_GET array

The $_GET array collects the values sent from a form using the HTTP GET method. When the user clicks the Submit button, all the information entered by the user in the form’s input fields form is stored in $_GET. The associative array stores the information in the form of key/value pairs. The keys are the names assigned to the form’s input text fields and the values are the data entered by the user in the respective keys. Thus, the data in the $_GET array can be accessed by specifying the key names. For example, information stored in the Userid key is accessed via $_GET["Userid"].

The target PHP script createuser.php can extract the data from a $_GET array using following code:

<?php

$uid =trim($_GET['Userid']);

$contactno =trim($_GET['ContactNo']);

$emailid =trim($_GET['EmailAddress']);

echo ‘Welcome ‘ . $uid . ‘!. Your account is created <br>’;

echo ‘Your Contact number is ‘ . $contactno . ‘<br>’;

echo ‘Your Email address  is ‘ .  $emailid . ‘<br>’;

?>

Note The period (.) in the echo command is used for concatenation. The trim() function truncates trailing spaces.

In this PHP script, we are using $_GET array to retrieve the information entered by the user in the form’s input text fields Userid, ContactNo and EmailAddress, and store them into the key variables $uid, $contactno, and $emailid respectively. The information stored in the key variables then displayed on the screen via the echo command.

$_POST array

The $_POST array collects the values sent from a form that is using the HTTP POST method. When the user clicks the Submit button, all the posted information will be stored in the $_POST associative array in the form of key/value pairs . The keys are the names assigned to the form’s input text fields. Information in the $_POST array is accessed by specifying the respective key. For example, to access the information stored in the Userid input field, we would use $_POST["Userid"].

The target PHP script createuser.php extracts the data from $_POST array and displays a welcome message in this bit of code:

<?php

$uid =trim($_POST['Userid']);

$contactno =trim($_POST['ContactNo']);

$emailid =trim($_POST['EmailAddress']);

echo ‘Welcome ‘ . $uid . ‘!. Your account is created <br>’;

?>

$_REQUEST array

The $_REQUEST array contains the contents of both $_GET and $_POST. That is, the array is used to collect information from a form that is sending data by either the GET or POST method. If don’t know which HTTP is being used by the form, it’s a good idea to use a $_REQUEST array.

In the following code fragment, the target PHP script, createuser.php, extracts the data from a $ REQUEST array and displays a message to the user.

<?php

$uid =trim($_REQUEST['Userid']);

$contactno =trim($_REQUEST['ContactNo']);

$emailid =trim($_REQUEST['EmailAddress']);

echo ‘Welcome ‘ . $uid . ‘!. Your account is created <br>’;

?>

When a user enters information in the form and selects the Submit button, the data in the input text fields is passed to the createuser.php PHP script (see Figure 1 (a)). We are using $_REQUEST array in the PHP script to retrieve the user-entered information, Userid, ContactNo and EmailAddress, and store it into the variables $uid, $contactno and $emailid respectively. The code also creates a Welcome panel containing a toolbar with a Back button and a welcome message, displayed in the panel body via the echo command, as shown in Figure 1 (b)

Note: When user selects the Submit button, the application automatically jumps from the current panel to the target PHP script. We don’t need to explicitly use a goTo function.

Figure 1 (a) Information entered in the form’s input fields (b) Welcome panel with toolbar and account confirmation message

For more information, refer to my book: “Beginning Web Development for Smartphones: Developing Web Applications with PHP, MySQL and jQTouch” available at  http://www.amazon.com/Beginning-Web-Development-Smartphones-ebook/dp/B0042X9CHI/ref=sr_1_2?ie=UTF8&m=AH9CGK6QR37LL&s=digital-text&qid=1284780488&sr=8-2

How to display Application Badges to an iPhone application

In this article, we will lean how to display an application badge to an iPhone application. Application Badges are usually seen in  the form of a number or message in red color on the top right corner of our application icon as shown in Figure 2 serving as a reminder of something that we have not yet completed in the application like reading of some  emails or notifications or some critical data etc. Since application badges are meant specially for reminder purpose, there is no need for the application to be running for displaying the application badge. Usually, the icon of our application (assuming our application name is appbadge) appears without any badge in the springboard as shown in Figure 1.

Figure 1  Application icon in springboard without application badge

To display an application badge, we make use of the applicationIconBadgeNumber property of the UIApplication class

Example:

[UIApplication sharedApplication].applicationIconBadgeNumber=10;

Above statement will display a value 10 as our application badge

Assuming our application name is appbadge, we write above statement in applicationDidFinishLaunching method to assign the badge as a numerical value 10 as shown in listing 1.

Listing 1.  Code in Application Delegate file: appbadgeAppDelegate.m

 

//  appbadgeAppDelegate.m

//  appbadge

#import “appbadgeAppDelegate.h”

#import “appbadgeViewController.h”

@implementation appbadgeAppDelegate

@synthesize window;

@synthesize viewController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

[UIApplication sharedApplication].applicationIconBadgeNumber=10;

[window addSubview:viewController.view];

[window makeKeyAndVisible];

}

- (void)dealloc {

[viewController release];

[window release];

[super dealloc];

}

@end

 

We can see that the statement that is displaying application badge in our application is :

[UIApplication sharedApplication].applicationIconBadgeNumber=10.

Let’s execute the application (by selecting Build and Run) from Xcode Project window to see how application badge appears. We see that the application badge appears with our application icon: appbadge as shown in Figure 2

Figure 2  Application Badge displayed with value 10

This finishes my article on “How to display Application Badges of iPhone application”

 

Validation in jQuery : Confirming a required field is not left blank

In this article, we are going to learn how to validate an input field. That is, if user leaves  any essential field blank, an error message will be displayed .

It’s a very common situation while developing web applications that you have an input text field in a form and you want that it should not be left blank and the user must enter the required data in it or else get an error message if it is left blank.

HTML file for “Validation in jQuery : Confirming a required field is not left blank”

Assume that you have  an HTML file that displays a form that contains a label, text field, error message and a submit button as shown below:

<body>

<form method=”post” action=”">

<div><span>User Id *</span><input  /><span> This field cannot be blank</span></div>

<input value=”Submit”>

</form>

</body>

Since the purpose of the HTML form above is to validate on the input text field and not to send the entered data to some other page for processing, the action attribute of the form is left blank. The form is assigned an id of signup  and method is  set to post – though it will have no effect on our validation procedure.

The label message User Id  is enclosed in a span element of class label. The input text field is assigned a class name infobox and the error message (“This field cannot be blank”) is stored as a span element of class error and finally the submit button is assigned the class submit.

The reasons of assigning the classes to all the four items of the form is to apply the properties defined in the class selectors .label, .infobox, .error and submit (defined in the style sheet: style.css) to be applied automatically to the respective four items of the form. The style sheet with the respective class selectors is as shown below:

style.css

.label {float: left; width: 120px; }

.infobox {width: 200px; }

.error { color: red; padding-left: 10px; }

.submit { margin-left: 125px; margin-top: 10px;}

Code for “Validation in jQuery : Confirming a required field is not left blank”

The jQuery code to confirm that the input text field is not left blank and to display an error message if it is left blank is as shown below:

$(document).ready(function() {

$(‘.error’).hide();

$(‘.submit’).click(function(event){

var data=$(‘.infobox’).val();

var len=data.length;

if(len<1)

{

$(‘.error’).show();

event.preventDefault();

}

else

{

$(‘.error’).hide();

}

});

});

The class selector .label has float property set to left so that the next item (input text field) may appear to its right and its width property is set to value 120px to give enough space for the label to display. The class selector .infobox has width property set to value 200px – the size of the input text field. The class  selector .error has color property set to red to highlight it, padding from left is set to 10px to keep the distance from the input text field . The class selector .submit has margin-left and margin-top property set to 125px and 10px respectively to set the distance from the left boundary of the browser window and from the input text field as we want the submit button to appear below the input text field.

In above jQuery code, we can see that initially, the error message is hidden and a click event is attached to the submit button. In the event handling function of the click event, we extract the data entered by the user in the input text field (assigned the class name infobox)  and store it in a variable: data. We find out the length of the data and if it is less than 1 (which means the user has not entered anything in the text field), we make the  error message visible on the screen. The method preventDefault() of the event object is used to avoid  the submit button from sending the data entered by the user to the server.

On execution of the jQuery code, we will get a form displayed with label, input text field and a submit button. If we select the submit button without entering anything in the text field, we will get an error message; ‘This field cannot be blank”  displayed in red beside the text field as shown in below given figure

Error message displayed when the input text field is left blank

If we enter some name in the text field, and select the submit button, we will not get any error message as is shown in below given figure.

No error message if data is entered in the input text field

This finishes my article on “Validation in jQuery : Confirming a required field is not left blank”

For more information, refer my book : “jQuery Recipes A Problem-Solution Approach” available at : http://www.amazon.com/jQuery-Recipes-Problem-Solution-Approach-Development/dp/1430227095/ref=sr_1_1?ie=UTF8&s=books&qid=1287320549&sr=8-1

Checking and unchecking all checkboxes together in jQuery

In this article, we will learn to check and uncheck all checkboxes together through a single checkbox.

We assume, we have several checkboxes in a form where each checkbox designates a food item being sold in a fast food corner. Above all the food item checkboxes, there is a checkbox : ‘Check All’; we will learn how to check all the checkboxes designating food items when user selects the ‘Select All’ checkbox. Similarly, we will see how all the checkboxes gets unchecked when ‘Select All’ checkbox is unchecked.

HTML file for  Checking and unchecking all checkboxes together in jQuery

Let us make an HTML file that displays five checkboxes (four for the food items and one for collectively checking and unchecking them). The HTML file may appear as shown below:

<body>

<form>

<div><input type=”checkbox”>Check/Uncheck all Checkboxes</div>

<div><input type=”checkbox”  id=”pizza” value=5>Pizza $5</div>

<div> <input type=”checkbox”  id=”hotdog” value=2>HotDog $2</div>

<div><input type=”checkbox”  id=”coke” value=1>Coke $1</div>

<div><input type=”checkbox”  id=”fries” value=3>French Fries $3</div>

</form>

</body>

In the HTML file, we can see that the five checkboxes are assigned the class name ‘infobox’ out of which four checkboxes represent the four items sold in a fast food corner: ‘Pizza’, ‘HotDog’, ‘Coke’ and ‘French Fries’ respectively along with their price.

In the style sheet, we define the class selector: ‘.infobox’ that may appear as shown below:

.infobox{ padding: 5px; }

The padding property is set to value 5px to create spacing among the checkboxes

Code for Checking and unchecking all checkboxes together in jQuery

The jQuery code to check and uncheck all the checkboxes on selecting the ‘Check All’ checkbox  and displaying the bill of the selected food items is as shown below:

$(document).ready(function() {

$(‘#checkall’).click(function(){

$(“input[type='checkbox']“).attr(‘checked’, $(‘#checkall’).is(‘:checked’));

});

$(‘form’).find(‘:checkbox’).click(function(){

var amt=0;

$(‘div’).filter(‘:gt(0)’).find(‘:checkbox’).each(function(){

if($(‘div:gt(0)’))

{

if($(this).is(‘:checked’))

{

amt=amt+parseInt($(this).val());

}

}

});

$(‘p’).remove();

$(‘<p>’).insertAfter(‘div:eq(4)’);

$(‘p’).text(‘Your bill is $ ‘+amt);

});

});

In the jQuery code, we attach a click event to ‘Check All’ checkbox (element of id: ‘checkall’). The statement:

$(“input[type='checkbox']“).attr(‘checked’, $(‘#checkall’).is(‘:checked’));

To understand above statement, let us have a small introduction of .attr() and .is() methods that are used in this statement :

.attr()

The .attr() method is used for setting the attributes of the selected element(s).

Syntax:

.attr(attribute, value)

.is()

The .is() method checks the selected element(s) with a selector and returns true if the any of the selected element matches with the selector else returns false.

Syntax:

.is(selector)

The part of above statement: $(‘#checkall’).is(‘:checked’) checks if the checkbox of id:’checkall’ is checked or not. If the checkbox (of id: ‘checkall’) is checked, .is() method will return true else it will return false.  If the .is() method returns true, all the input elements of type:’checkbox’ (i.e. all the checkboxes) are set to checked mode and obviously all of them will be set to unchecked mode if the .is() method returns false . Since, user is also allowed to check any individual check box, we check the status of each checkbox that is with index value greater than 0 (because the checkbox with index value 0 is the ‘CheckAll’ checkbox). The value of all the checkboxes is added to the to the ‘amt’ variable. To display the bill, we create a paragraph element and add the text: ‘Your bill is amt’ (where amt is the numerical value contained in the amt variable) and insert this paragraph element after the div element of index value  4 i.e. after the last checkbox.

If we select ‘Check All’’ checkbox, all the following checkboxes will be checked and the addition of their value is displayed in the form of bill as shown in below given figure :

All checkboxes selected by selecting ‘Check/Uncheck all Checkboxes’ checkbox

If ‘Check All’ checkbox is unchecked, all the following checkboxes will be unchecked and hence the bill of amount 0 $ will be displayed as shown in below given figure :

All checkboxes un selected by unchecking ‘Check/Uncheck all Checkboxes’ checkbox

User can also select any of the individual checkbox. The total amount of the selected checkboxes will appear as shown in below given figure :

Bill of the individually selected food items displayed

This finishes my article on “Checking and unchecking all checkboxes together in jQuery”

For more information, refer my book : “jQuery Recipes A Problem-Solution Approach” available at : http://www.amazon.com/jQuery-Recipes-Problem-Solution-Approach-Development/dp/1430227095/ref=sr_1_1?ie=UTF8&s=books&qid=1287320549&sr=8-1

iPhone Web Application : How to create “Create Account” panel in jQTouch

Almost in all web applications, we find a Sign Up or Register link asking visitors to create an account. The information entered by the visitor while creating the account is stored in server side database. The user information is fetched from the server side tables when user Signs In the application in future.

In this artcle, we will learn how to take data from the user and pass it onto the server side PHP script for display. To save the user entered information into server side database table, wait for my next article.

The following code fragment creates the Create Account panel. The code takes user-entered data and sends it to a PHP script.

<div>

<div class=”toolbar”>

<h1>Create Account</h1>

<a href=”#”>Cancel</a>

</div>

<form action=”createuser.php” method=”POST” class=”form”>

<ul class=”rounded”>

<li><input type=”text” placeholder=”Userid”/>

</li>

<li><input type=”text” placeholder=”Contact No”/></li>

<li><input type=”text” placeholder=”Email

Address”/></li>

</ul>

<a href=”#” class=”submit whiteButton”>Submit</a>

</form>

</div>

We can see that when the Submit button is pressed, we will go to the createuser.php file. The HTTP Request method used for passing the information entered in the input text fields is POST. That is, when the user fills out the form and clicks Submit, the $_POST["Userid"], $_POST["ContactNo"], and $_POST["EmailAddress"] elements will be automatically initialized with the data entered by the user in the input text fields named Userid, ContactNo, and EmailAddress respectively.

The information in the $_POST array can be retrieved by the createuser.php target script, which performs the following tasks:

  • Extracts information from the $_POST array
  • Creates a new panel for displaying extracted information
  • Displays a message to the user

The script is shown below.

<?php

$uid =trim($_REQUEST['Userid']);

$contactno =trim($_REQUEST['ContactNo']);

$emailid =trim($_REQUEST['EmailAddress']);

?>

<div>

<div class=”toolbar”>

<h1>Welcome</h1>

<a href=”#”>Back</a>

</div>

<?php

echo ‘Welcome ‘ . $uid . ‘!. Your account is created ‘;

?>

</div>

Note: Remember that the $_REQUEST array can be used for extracting information sent by a form, regardless of the method (GET or POST) used by the form.

When a user enters information in the form and selects the Submit button, the data in the input text fields is passed to the createuser.php PHP script, as shown in Figure 1(a). We are using $_REQUEST array in the PHP script to retrieve the user-entered information, Userid, ContactNo, and EmailAddress, and store it into the variables $uid, $contactno, and $emailid respectively. The code also creates a Welcome panel containing a toolbar with a Back button and a welcome message, displayed in the panel body via the echo command, as shown in Figure 1(b).

Note: When user selects the Submit button, the application automatically jumps from the current panel to the target PHP script. We don’t need to explicitly use a goTo function.

Figure 1 (a) Information entered in the form’s input fields (b) Welcome panel with toolbar and account confirmation message

For more information, refer  my book: “Beginning Web Development for Smartphones: Developing Web Applications with PHP, MySQL and jQTouch” available at amazon: