Apress Pro PHP and jQuery
■ 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
■ 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
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
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
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 Data base 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 Vi ew 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
Planning the Application’s Folder Structure .................................................................................... ..... 120
Modifying the Development Environment............................................................................................. 122
Building the Calendar .................................................................................................... 124
Creating the Database .......................................................................................................................... 124
Connecting to the Data base 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 Vi ew 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
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 a nd Adding an Active Class................................................................... 240
Extracting the Query String with Regular Expressions ......................................................................... 24 1
■ 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 a nd Adding an Active Class................................................................... 240
Extracting the Query String with Regular Expressions ......................................................................... 24 1
Creating a Modal Window........................................................................................................ ............. 243
Retrieve and Display Event Information with AJAX .............................................................................. 247
Add a Close Button ............................................................................................................. .................. 253
Add Effects to the Creation and Destruction of the Modal Window...................................................... 254
Summary ....................................................................................................................... 262
■ Chapter 8: Editing the Calendar with AJAX and jQuery .......................................263
Opening the Event Creation Form .................................................................................. 263
Adding an AJAX Call to Load the Form ................................................................................................. 264
Modifying the AJAX Processing File to Load the Form ......................................................................... 265
Making the Cancel Button Behave Like the Close Button..................................................................... 268
Saving New Events in the Database .............................................................................. 269
Modifying the AJAX Processing File to Handle New Submissions ....................................................... 271
Adding Events Without Refreshing ................................................................................ 273
Deserializing the Form Data .................................................................................................... ............. 274
Creating Date Objects.......................................................................................................... ................. 279
Appending the Event to the Calendar ................................................................................................... 283
Getting the New Event’s ID ................................................................................................................... 286
Editing Events in a Modal Window................................................................................. 290
Determining the Form Action.................................................................................................... ............ 291
Storing the Event ID if One Exists ............................................................................................. ............ 292
Remove Event Data from the Modal Window ....................................................................................... 294
Ensuring Only New Events Are Added to the Calendar......................................................................... 296
Confirming Deletion in a Modal Window........................................................................ 298
Displaying the Confirmation Dialog ............................................................................................. ......... 298
Configuring the Form Submission Event Handler for Deletion ............................................................. 301
Remove the Event from the Calendar After Deletion ............................................................................ 304
Summary ....................................................................................................................... 307
■ PART 4: Advancing jQuery and PHP.....................................................................309
Retrieve and Display Event Information with AJAX .............................................................................. 247
Add a Close Button ............................................................................................................. .................. 253
Add Effects to the Creation and Destruction of the Modal Window...................................................... 254
Summary ....................................................................................................................... 262
■ Chapter 8: Editing the Calendar with AJAX and jQuery .......................................263
Opening the Event Creation Form .................................................................................. 263
Adding an AJAX Call to Load the Form ................................................................................................. 264
Modifying the AJAX Processing File to Load the Form ......................................................................... 265
Making the Cancel Button Behave Like the Close Button..................................................................... 268
Saving New Events in the Database .............................................................................. 269
Modifying the AJAX Processing File to Handle New Submissions ....................................................... 271
Adding Events Without Refreshing ................................................................................ 273
Deserializing the Form Data .................................................................................................... ............. 274
Creating Date Objects.......................................................................................................... ................. 279
Appending the Event to the Calendar ................................................................................................... 283
Getting the New Event’s ID ................................................................................................................... 286
Editing Events in a Modal Window................................................................................. 290
Determining the Form Action.................................................................................................... ............ 291
Storing the Event ID if One Exists ............................................................................................. ............ 292
Remove Event Data from the Modal Window ....................................................................................... 294
Ensuring Only New Events Are Added to the Calendar......................................................................... 296
Confirming Deletion in a Modal Window........................................................................ 298
Displaying the Confirmation Dialog ............................................................................................. ......... 298
Configuring the Form Submission Event Handler for Deletion ............................................................. 301
Remove the Event from the Calendar After Deletion ............................................................................ 304
Summary ....................................................................................................................... 307
■ PART 4: Advancing jQuery and PHP.....................................................................309
■ Chapter 9: Performing Form Validation with Regular Expressions .....................311
Getting Comfortable with Regular Expressions ............................................................. 311
Understanding Basic Regular Expression Syntax ................................................................................. 311
Drilling Down on the Basics of Pattern Modifiers ................................................................................. 316
Getting Fancy with Backreferences.............................................................................................. ........ 318
Matching Character Classes..................................................................................................... ............ 320
Finding Word Boundaries ........................................................................................................ ............. 323
Using Repetition Operators................................................................................................................... 323
Detecting the Beginning or End of a String ..................................................................................... ..... 324
Using Alternation .................................................................................................................................. 324
Using Optional Items............................................................................................................................. 325
Putting It All Together ........................................................................................................................... 326
Adding Server-Side Date Validation............................................................................... 328
Defining the Regex Pattern to Validate Dates................................................................................... .... 328
Adding a Validation Method to the Calendar Class............................................................................... 333
Returning an Error if the Dates Don’t Validate................................................................................. ..... 334
Adding Client-Side Date Validation ................................................................................ 338
Creating a New JavaScript File to Validate the Date String.................................................................. 338
Including the New File in the Footer........................................................................................... .......... 339
Preventing the Form Submission if Validation Fails ............................................................................. 339
Summary ....................................................................................................................... 343
■ Chapter 10: Extending jQuery ..............................................................................345
Adding Functions to jQuery............................................................................................ 345
Adding Your Date Validation Function to jQuery ................................................................................... 345
Modifying the Include Script................................................................................................................. 348
Modifying the Initialization Script ............................................................................................ ............. 348
Adding Methods to jQuery ............................................................................................. 350
Getting Comfortable with Regular Expressions ............................................................. 311
Understanding Basic Regular Expression Syntax ................................................................................. 311
Drilling Down on the Basics of Pattern Modifiers ................................................................................. 316
Getting Fancy with Backreferences.............................................................................................. ........ 318
Matching Character Classes..................................................................................................... ............ 320
Finding Word Boundaries ........................................................................................................ ............. 323
Using Repetition Operators................................................................................................................... 323
Detecting the Beginning or End of a String ..................................................................................... ..... 324
Using Alternation .................................................................................................................................. 324
Using Optional Items............................................................................................................................. 325
Putting It All Together ........................................................................................................................... 326
Adding Server-Side Date Validation............................................................................... 328
Defining the Regex Pattern to Validate Dates................................................................................... .... 328
Adding a Validation Method to the Calendar Class............................................................................... 333
Returning an Error if the Dates Don’t Validate................................................................................. ..... 334
Adding Client-Side Date Validation ................................................................................ 338
Creating a New JavaScript File to Validate the Date String.................................................................. 338
Including the New File in the Footer........................................................................................... .......... 339
Preventing the Form Submission if Validation Fails ............................................................................. 339
Summary ....................................................................................................................... 343
■ Chapter 10: Extending jQuery ..............................................................................345
Adding Functions to jQuery............................................................................................ 345
Adding Your Date Validation Function to jQuery ................................................................................... 345
Modifying the Include Script................................................................................................................. 348
Modifying the Initialization Script ............................................................................................ ............. 348
Adding Methods to jQuery ............................................................................................. 350
Building Your Plugin ............................................................................................................................. 351
Implementing Your Plugin ....................................................................................................... ............. 357
Summary ....................................................................................................................... 360
■ Index...................................................................................................................361
Implementing Your Plugin ....................................................................................................... ............. 357
Summary ....................................................................................................................... 360
■ Index...................................................................................................................361
Enregistrer un commentaire