CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN OPEN SOURCE Companion eBook Available Pro PHP and jQuery Pro Dear Reader, In Pro PHP and jQuery, you’ll learn everything you need to know to start develop- P ing powerful applications using the power of jQuery, AJAX and object-oriented PHP. This book will show you the ropes and get you developing with advanced H Pro PHP development in combination with progressive enhancement techniques in jQuery to build highly interactive user interfaces for your applications. P Jason Lengstorf, Author of As you work through the sample application in this book, I'll teach you the PHP for Absolute Beginners essentials of object-oriented PHP and get you started in jQuery from an absolute PHP beginner's level. You'll learn everything you need to know to start building out- a standing user interfaces, including: n and • the basics of the powerful jQuery library d • object-oriented PHP • AJAX-powered user interface design j • extending the jQuery library with custom plugins Q • form validation with regular expressions jQuery Web development is quickly becoming the medium of choice for new applica- u tions, and your ability to create online apps with the look and feel of desktop apps can make the difference between a good interface and a great interface. e Along the way you'll learn useful tricks to improve your web development, and in no time you'll be creating fantastic, user-friendly, AJAX-powered applications. r y Jason Lengstorf THE APRESS ROADMAP PHP Objects, Pro PHP: Patterns, and Practice, Patterns, Frameworks, PHP for Third Edition Testing, and More Companion eBook Absolute Beginners PHP Object-Oriented Pro PHP Add quick, smooth, and easy interactivity Solutions Refactoring to your PHP sites with jQuery Beginning PHP and MySQL, Practical Web 2.0 Third Edition Applications with PHP Pro PHP and jQuery See last page for details on $10 eBook version L e Jason Lengstorf SOURCE CODE ONLINE n g www.apress.com ISBN 978-1-4302-2847-9 s 54999 to r f US $49.99 Shelve in: PHP User level: 9 781430 228479 Intermediate–Advanced this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.75" 400 page count Pro PHP and jQuery ■ ■ ■ JASON LENGSTORF Pro PHP and jQuery Copyright © 2010 by Jason Lengstorf All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-2847-9 ISBN-13 (electronic): 978-1-4302-2848-6 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Michelle Lowman Technical Reviewer: Robert Banh Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anita Castro Copy Editor: Patrick Meader and Heather Lang Compositor: Kimberly Burton Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. ii For Nate. It's 2-1 now. Contents at a Glance ■About the Author.......................................................................................................xii ■About the Technical Reviewer..................................................................................xiii ■Acknowledgements..................................................................................................xiv ■PART 1: Getting Comfortable with jQuery....................................................................1 ■Chapter 1: Introducing jQuery.....................................................................................3 ■Chapter 2: Common jQuery Actions and Methods.....................................................25 ■PART 2: Getting Into Advanced PHP Programming...................................................85 ■Chapter 3: Object-Oriented Programming.................................................................87 ■Chapter 4: Build an Events Calendar.......................................................................119 ■Chapter 5: Add Controls to Create, Edit, and Delete Events...................................167 ■Chapter 6: Password Protecting Sensitive Actions and Areas...............................199 ■PART 3: Combining jQuery with PHP Applications..................................................233 ■Chapter 7: Enhancing the User Interface with jQuery............................................235 ■Chapter 8: Editing the Calendar with AJAX and jQuery...........................................263 ■PART 4: Advancing jQuery and PHP.........................................................................309 ■Chapter 9: Performing Form Validation with Regular Expressions.........................311 ■Chapter 10: Extending jQuery..................................................................................345 ■ Index.......................................................................................................................361 iv Contents ■About the Author...................................................................................................xii ■About the Technical Reviewer..............................................................................xiii ■Acknowledgements..............................................................................................xiv ■PART 1: Getting Comfortable with jQuery................................................................1 ■Chapter 1: Introducing jQuery.................................................................................3 Choosing jQuery over JavaScript.......................................................................................3 Understanding JavaScript Libraries.........................................................................................................3 Understanding the Benefits of jQuery.......................................................................................................4 Understanding the History of jQuery.........................................................................................................4 Setting Up a Testing Environment.....................................................................................4 Installing Firefox.......................................................................................................................................5 Installing Firebug......................................................................................................................................5 Including jQuery in Web Pages..........................................................................................7 Including a Downloaded Copy of the jQuery Library.................................................................................7 Including a Remotely Hosted Copy of the jQuery Library.........................................................................8 Using the Google AJAX Libraries API........................................................................................................8 Setting up a Test File.........................................................................................................8 Introducing the jQuery Function ($)..........................................................................................................9 Selecting DOM Elements Using CSS Syntax...........................................................................................10 Summary.........................................................................................................................23 ■Chapter 2: Common jQuery Actions and Methods.................................................25 Understanding the Basic Behavior of jQuery Scripts.......................................................25 v ■ CONTENTS Understanding jQuery Methods.......................................................................................25 Traversing DOM Elements......................................................................................................................26 Creating and Inserting DOM Elements....................................................................................................36 Accessing and Modifying CSS and Attributes.........................................................................................53 Affecting Result Sets..............................................................................................................................62 Using Animation and Other Effects.........................................................................................................65 Handling Events......................................................................................................................................71 Using AJAX Controls...............................................................................................................................78 Summary.........................................................................................................................84 ■PART 2: Getting Into Advanced PHP Programming...............................................85 ■Chapter 3: Object-Oriented Programming.............................................................87 Understanding Object-Oriented Programming.................................................................87 Understanding Objects and Classes................................................................................87 Recognizing the Differences Between Objects and Classes..................................................................88 Structuring Classes................................................................................................................................88 Defining Class Properties.......................................................................................................................89 Defining Class Methods..........................................................................................................................90 Using Class Inheritance..........................................................................................................................99 Assigning the Visibility of Properties and Methods..............................................................................103 Commenting with DocBlocks................................................................................................................110 Comparing Object-Oriented and Procedural Code.........................................................112 Ease of Implementation........................................................................................................................112 Better Organization...............................................................................................................................117 Easier Maintenance..............................................................................................................................117 Summary.......................................................................................................................117 ■Chapter 4: Build an Events Calendar...................................................................119 Planning the Calendar....................................................................................................119 Defining the Database Structure..........................................................................................................119 vi ■ CONTENTS Creating the Class Map........................................................................................................................119 Planning the Application’s Folder Structure.........................................................................................120 Modifying the Development Environment.............................................................................................122 Building the Calendar....................................................................................................124 Creating the Database..........................................................................................................................124 Connecting to the Database with a Class.............................................................................................125 Creating the Class Wrapper..................................................................................................................127 Adding Class Properties.......................................................................................................................127 Building the Constructor.......................................................................................................................129 Loading Events Data.............................................................................................................................136 Outputting HTML to Display the Calendar and Events..........................................................................143 Outputing HTML to Display Full Event Descriptions..............................................................................160 Summary.......................................................................................................................166 ■Chapter 5: Add Controls to Create, Edit, and Delete Events ...............................167 Generating a Form to Create or Edit Events...................................................................167 Adding a Token to the Form.................................................................................................................169 Creating a File to Display the Form......................................................................................................171 Adding a New Stylesheet for Administrative Features.........................................................................172 Saving New Events in the Database..............................................................................176 Adding a Processing File to Call the Processing Method.....................................................................179 Adding a Button to the Main View to Create New Events.....................................................................181 Adding Edit Controls to the Full Event View...................................................................185 Modifying the Full Event Display Method to Show Admin Controls......................................................187 Adding the Admin Stylesheet to the Full Event View Page...................................................................188 Deleting Events..............................................................................................................190 Generating a Delete Button..................................................................................................................191 Creating a Method to Require Confirmation.........................................................................................192 Creating a File to Display the Confirmation Form.................................................................................195 vii ■ CONTENTS Summary.......................................................................................................................198 ■Chapter 6: Password Protecting Sensitive Actions and Areas...........................199 Building the Admin Table in the Database.....................................................................199 Building a File to Display a Login Form.........................................................................200 Creating the Admin Class..............................................................................................202 Defining the Class.................................................................................................................................202 Building a Method to Check the Login Credentials...............................................................................203 Modifying the App to Handle the Login Form Submission....................................................................213 Allowing the User to Log Out.........................................................................................218 Adding a Log Out Button to the Calendar.............................................................................................218 Creating a Method to Process the Logout............................................................................................220 Modifying the App to Handle the User Logout......................................................................................221 Displaying Admin Tools Only to Administrators.............................................................223 Showing Admin Options to Administrators...........................................................................................223 Limiting Access to Administrative Pages.............................................................................................228 Summary.......................................................................................................................231 ■PART 3: Combining jQuery with PHP Applications..............................................233 ■Chapter 7: Enhancing the User Interface with jQuery........................................235 Adding Progressive Enhancements with jQuery............................................................235 Setting Progressive Enhancement Goals..............................................................................................236 Include jQuery in the Calendar App...............................................................................236 Create a JavaScript Initialization File...................................................................................................237 Creating a New Stylesheet for Elements Created by jQuery.................................................................238 Creating a Modal Window for Event Data......................................................................240 Binding a Function to the Click Event of Title Links.............................................................................240 Preventing the Default Action and Adding an Active Class...................................................................240 Extracting the Query String with Regular Expressions.........................................................................241 viii
Description: