Book Image

PHP jQuery Cookbook

By : Vijay Joshi
Book Image

PHP jQuery Cookbook

By: Vijay Joshi

Overview of this book

As web technology evolves, the gap between desktop applications and web applications continues to vanish. And what better way to bridge that gap, for your own website, than using the best two open source technologies in the market: PHP and jQuery. The power-combo of these two is leading the way in revolutionizing the RIA world. Although they are easy to integrate, finding or figuring out how to do what you want to do is rather arduous.The PHP jQuery Cookbook will be your handy guide with walkthroughs of common integration tasks and problems that will help you master the possibilities available using the PHP and jQuery combo. You will learn quick solutions to necessary tasks to integrate the power of two of the best known and most widely used web technologies of today – PHP on the server side and jQuery on the client side. Glide through the basics and move to advanced topics to create dynamic and interactive web applications with this book in hand.This book covers a wide array of technical aspects of creating an interactive website. Apart from basics of PHP and jQuery, you will go through advanced topics like creating plugins, validating controls, and useful utilities that you will be able to use as stand-alone tools. AJAX, the key technique of browser-server communication is covered in detail. You will also learn to use JSON, which is becoming preferred as a mode of data interchange over XML, both in web applications and web services.The book also covers database interaction, which is an important part of any dynamic web application. You will also gain expertise in debugging JavaScript with the help of useful tools that will save you hours of tedious manual debugging.Most importantly, by using jQuery and PHP together, you will be able to develop applications that are compatible with all major browsers, with no need to write code targeted at specific browsers!
Table of Contents (17 chapters)
PHP jQuery Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating keyboard shortcuts


Keyboard navigation is common in window-based applications. This is very handy for those who prefer keyboard controls over mouse controls. Keyboard shortcuts can also be created in web applications but they are difficult to implement due to inconsistency among browsers.

We will create a simple example in this recipe that will give you the basic understanding of implementing shortcut keys. You will be able to create your own shortcut keys for use in your web applications.

Getting ready

Get the jQuery library to use with this recipe.

How to do it...

  1. Create a new file named keyboard.html and save it in the chapter1 directory.

  2. In the body of HTML create two DIV elements and in the <head> section write some CSS to apply styles to these DIV elements.

    <html>
      <head>
        <title>Keyboard Shortcuts</title>
        <style type="text/css">
        div{ border : 1px solid black;float:left;height:200px; margin:10px; width:220px;}
        </style>
      </head>
      <body>
        <div>You can toggle this div using Alt+S</div>
        
        <div>You can toggle this div using Alt+G </div>
        
        <p style="clear:both;">&nbsp;</p>
        <p>Press Alt+B to toggle both divs</p>
      </body>
    </html>
  3. Write the jQuery code that will create keyboard shortcuts to toggle these DIV elements. The keydown event handler will be used to implement this behaviour. It will check for the keys that are pressed and then take actions accordingly. Three shortcuts will be created. Pressing Alt + S will toggle the first DIV. Alt + G will toggle the second DIV. Pressing Alt + B will toggle both the DIV elements together.

    Another handler keyup will be used to reset the required variables.

    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
      $(document).ready(function ()
      {
        //remember that this is a global variable
        var altPressed = false;
        $(document).keydown(function (event) 
        {
          if(event.which == 18) 
            altPressed = true;
          if(altPressed)
          {
             switch(event.which)
            {
               case 83:
                   $('div:first').slideToggle('slow');
                   return false;
                   break;
               case 71:
                   $('div:last').slideToggle('slow');
                   return false;
                   break;
               case 66:
                   $('div').slideToggle('slow');
                   return false;
                   break;
            }
          }
        });
    
        $(document).keyup(function (event) {
          if(event.which == 18) 
            altPressed = false;
        });
      });
    </script>
  4. Open your browser and run the keyboard.html file. Try pressing the shortcuts that we have just created. You will see that the DIV elements will toggle with a slide effect.

How it works...

In order to be able to create shortcut keys, first we need to find out which key was pressed. Different browsers have their own methods of determining the value of the pressed key. jQuery normalizes the way this information can be retrieved across browsers. An event object is available to handler functions. This event object has a property which that gives the code of the pressed key. Alt key has the value 18.

The keyboard shortcuts in this recipe use the combination of Alt and the other keys. We begin by declaring a global variable altPressed with the value set to false. Then there are two events attached to the page. keydown will execute when a key is in a pressed state and keyup when a key is released. Whenever Alt is pressed the keydown event will set its value to true. When released, it will be reset to false again by the keyup handler function.

Next comes the if statement, which will evaluate to a true value if the Alt key is pressed. If Alt is pressed and another key is pressed along with it, the switch case will check the key's value and will execute the corresponding switch case.

The value for the S key is 83. So, pressing S along with Alt will select the first DIV and will apply the slideToggle effect to it. Similarly, Alt + G will toggle the second DIV and Alt + B will toggle both DIVs.

Note

Note the return of false in each case of switch statement. Returning false is necessary to override a browser's default behavior. If false is not returned, pressing the Alt key will activate the browser's menu.

There's more...

List of common key codes

A list of key codes can be found at http://goo.gl/v2Fk

See also

  • Binding and unbinding elements in this chapter explains how to attach events to elements.