Pro PHP and jQuery (eBook)
400 Seiten
Apress (Verlag)
978-1-4302-2848-6 (ISBN)
This book is for intermediate programmers interested in building Ajax web applications using jQuery and PHP. Along with teaching some advanced PHP techniques, it will show you how to take your dynamic applications to the next level by adding a JavaScript layer with jQuery.
- Learn to utilize built-in PHP functions to build calendar tools.
- Learn how jQuery can be used for Ajax, animation, client-side validation, and more.
Jason Lengstorf is a 27-year-old turbogeek from Portland, OR. He started building websites in his late teens when his band couldn t afford to pay someone to do it, and he continued building websites after he realized his band wasn t actually very good. He s been a full-time freelance web developer since 2007, and expanded his business under the name Copter Labs, which is now a distributed freelance collective, keeping about 10 freelancers worldwide busy. He is also the author of PHP for Absolute Beginners and Pro PHP and jQuery.
At this point, you should feel comfortable with the object-oriented programming style. The whole core of the event calendar's backend will be based on OOP, so any concepts that may currently seem unclear will be more thoroughly examined as the concepts from this chapter are put into a practical, real-world example. In the next chapter, you'll start building the backend of the events calendar. 118 C H A P T E R 4 ? ? ? Build an Events Calendar Now that you're up to speed on the concept of object-oriented programming, you can start working on the project that will be the meat and potatoes of this book: the events calendar. It all starts here, and as this book progresses, you'll be adding more and more functionality using both PHP and jQuery. Planning the Calendar Because you're starting from absolute scratch, you need to take a minute to plan the application. This application will be database-driven (using MySQL), so the planning will take part in two stages: first the database structure and then a basic map of the application that will access and modify the database. Defining the Database Structure To make building the application much easier, the first thing you should plan is how the data will be stored. This shapes everything in the application.
Jason Lengstorf is a 27-year-old turbogeek from Portland, OR. He started building websites in his late teens when his band couldn t afford to pay someone to do it, and he continued building websites after he realized his band wasn t actually very good. He s been a full-time freelance web developer since 2007, and expanded his business under the name Copter Labs, which is now a distributed freelance collective, keeping about 10 freelancers worldwide busy. He is also the author of PHP for Absolute Beginners and Pro PHP and jQuery.
Title Page 1
Copyright page 2
Contents at a Glance 3
Table of Contents 4
About the Author 11
About the Technical Reviewer 12
Acknowledgments 13
PART 1 Getting Comfortable with jQuery 14
CHAPTER 1 Introducing jQuery 15
Choosing jQuery over JavaScript 15
Understanding JavaScript Libraries 15
Understanding the Benefits of jQuery 16
Understanding the History of jQuery 16
Setting Up a Testing Environment 16
Installing Firefox 17
Installing Firebug 17
Including jQuery in Web Pages 19
Including a Downloaded Copy of the jQuery Library 19
Including a Remotely Hosted Copy of the jQuery Library 20
Using the Google AJAX Libraries API 20
Setting up a Test File 20
Introducing the jQuery Function ($) 21
Selecting DOM Elements Using CSS Syntax 22
Basic Selectors 23
Hierarchy Selectors 25
Basic Filters 27
Content Filters 29
Visibility Filters 31
Attribute Filters 31
Child Filters 32
Form Filters 33
Summary 35
CHAPTER 2 Common jQuery Actions and Methods 36
Understanding the Basic Behavior of jQuery Scripts 36
Understanding jQuery Methods 36
Traversing DOM Elements 37
.eq() 37
.filter() and .not() 38
.first() and .last() 38
.has() 39
.is() 39
.slice() 39
.children() 40
.closest() 41
.find() 41
.next(), .nextAll(), and .nextUntil() 41
.prev(), .prevAll(), and .prevUntil() 43
.siblings() 43
.parent() 44
.parents() and .parentsUntil() 44
.add() 45
.andSelf() 46
.contents() 46
.end() 47
Creating and Inserting DOM Elements 47
Creating New DOM Elements 49
Inserting New Elements into the DOM 50
.remove() and .detach() 63
Accessing and Modifying CSS and Attributes 64
.attr() 64
.removeAttr() 65
.css() 66
.val() 68
.data() 69
.addClass(), .removeClass(), and .toggleClass() 70
.hasClass() 71
.height() and .width() 72
.innerHeight(), .innerWidth(), .outerHeight(), and .outerWidth() 73
Affecting Result Sets 73
.map() and .each() 74
Using Animation and Other Effects 76
.show() and .hide() 76
.fadeIn(), .fadeOut(), and .fadeTo() 77
.slideUp(), .slideDown(), and .slideToggle() 78
.animate() 78
.delay() 81
.stop() 81
Handling Events 82
Browser Events 82
Handling Document Loading Events 83
Handling Event Attachment 84
Shortcut Event Methods 88
Using AJAX Controls 89
$.ajax() 89
$.ajaxSetup() 91
Using Shorthand AJAX Methods 93
Summary 95
PART 2 Getting Into Advanced PHP Programming 96
CHAPTER 3 Object-Oriented Programming 97
Understanding Object-Oriented Programming 97
Understanding Objects and Classes 97
Recognizing the Differences Between Objects and Classes 98
Structuring Classes 98
Defining Class Properties 99
Defining Class Methods 100
Using Constructors and Destructors 103
Converting to a String 106
Using Class Inheritance 109
Overwriting Inherited Properties and Methods 110
Preserving Original Method Functionality While Overwriting Methods 112
Assigning the Visibility of Properties and Methods 113
Public Properties and Methods 113
Protected Properties and Methods 114
Private Properties and Methods 117
Static Properties and Methods 118
Commenting with DocBlocks 120
Comparing Object-Oriented and Procedural Code 122
Ease of Implementation 123
The Procedural Approach 123
The OOP Approach 125
Better Organization 127
Easier Maintenance 127
Summary 128
CHAPTER 4 Build an Events Calendar 129
Planning the Calendar 129
Defining the Database Structure 129
Creating the Class Map 129
Planning the Application’s Folder Structure 130
Public Files 130
Nonpublic Application Files 131
Modifying the Development Environment 132
Local Development 132
Remote Development 134
Building the Calendar 134
Creating the Database 134
Connecting to the Database with a Class 135
Creating the Class Wrapper 137
Adding Class Properties 137
Building the Constructor 139
Checking the Database Connection 140
Creating an Initialization File 142
Creating an Index File to Pull It All Together 143
Setting Basic Properties 144
Loading Events Data 146
Creating an Array of Event Objects for Use in the Calendar 149
Creating an Event Class 150
Creating the Method to Store Event Objects in an Array 152
Outputting HTML to Display the Calendar and Events 153
Modifying the Index File 155
Building the Calendar 156
Displaying Events in the Calendar 161
Making the Calendar Look Like a Calendar 164
Creating the Common Files—Header and Footer 168
Adding the Files to the Index 168
Outputing HTML to Display Full Event Descriptions 170
Creating a Method to Format Single Event Data 170
Creating a Method to Generate Markup 172
Creating a New File to Display Full Events 174
Summary 176
CHAPTER 5 Add Controls to Create, Edit, and Delete Events 177
Generating a Form to Create or Edit Events 177
Adding a Token to the Form 179
Creating a File to Display the Form 181
Adding a New Stylesheet for Administrative Features 182
Saving New Events in the Database 186
Adding a Processing File to Call the Processing Method 189
Adding a Button to the Main View to Create New Events 191
Adding Edit Controls to the Full Event View 195
Modifying the Full Event Display Method to Show Admin Controls 197
Adding the Admin Stylesheet to the Full Event View Page 198
Deleting Events 200
Generating a Delete Button 201
Creating a Method to Require Confirmation 202
Creating a File to Display the Confirmation Form 205
Summary 208
CHAPTER 6 Password Protecting Sensitive Actions and Areas 209
Building the Admin Table in the Database 209
Building a File to Display a Login Form 210
Creating the Admin Class 212
Defining the Class 212
Building a Method to Check the Login Credentials 213
Building a Method to Create Salted Hashes 217
Creating a Test Method for Salted Hashes 220
Creating a User to Test Administrative Access 222
Modifying the App to Handle the Login Form Submission 223
Allowing the User to Log Out 228
Adding a Log Out Button to the Calendar 228
Creating a Method to Process the Logout 230
Modifying the App to Handle the User Logout 231
Displaying Admin Tools Only to Administrators 233
Showing Admin Options to Administrators 233
Modifying the General Admin Options Method 234
Modifying the Event Options Method 236
Limiting Access to Administrative Pages 238
Disallowing Access to the Event Creation Form Without Login 238
Ensuring Only Logged In Users Can Delete Events 239
Summary 241
PART 3 Combining jQuery with PHP Applications 242
CHAPTER 7 Enhancing the User Interface with jQuery 243
Adding Progressive Enhancements with jQuery 243
Setting Progressive Enhancement Goals 244
Include jQuery in the Calendar App 244
Create a JavaScript Initialization File 245
Include the Initialization File in the Application 245
Ensuring the Document Is Ready Before Script Execution 245
Creating a New Stylesheet for Elements Created by jQuery 245
Including the Stylesheet in the Index File 247
Creating a Modal Window for Event Data 248
Binding a Function to the Click Event of Title Links 248
Preventing the Default Action and Adding an Active Class 248
Extracting the Query String with Regular Expressions 249
Using the Lazy Approach: String-Based Replacement 249
Adopting a Better Solution: Regular Expressions 249
Incorporating a Regular Expression into a Script 250
Creating a Modal Window 251
Creating the Utility Function to Check for a Modal Window 251
Calling the Utility Function from the Event Handler 253
Retrieve and Display Event Information with AJAX 255
Creating a File to Handle AJAX Requests 256
Loading Event Data Using AJAX 258
Add a Close Button 261
Add Effects to the Creation and Destruction of the Modal Window 262
Fade Out the Modal Window 263
Adding an Overlay and Fade in the Modal Window 265
Summary 270
CHAPTER 8 Editing the Calendar with AJAX and jQuery 271
Opening the Event Creation Form 271
Adding an AJAX Call to Load the Form 272
Modifying the AJAX Processing File to Load the Form 273
Making the Cancel Button Behave Like the Close Button 276
Saving New Events in the Database 277
Serializing the Form Data 278
Submitting the Serialized Form Data to the Processing File 279
Modifying the AJAX Processing File to Handle New Submissions 279
Adding Events Without Refreshing 281
Deserializing the Form Data 282
Decode Any URL-Encoded Characters in Form Values 284
Bring It All Together 286
Creating Date Objects 287
Modifying the Calendar Class with an ID 287
Building Date Objects in JavaScript 288
Fixing Timezone Inconsistencies 289
Ensuring the Event Occurs in the Current Month 290
Appending the Event to the Calendar 291
Getting the New Event’s ID 294
Modifying the Event Creation Method to Return New Event IDs 295
Editing Events in a Modal Window 298
Determining the Form Action 299
Storing the Event ID if One Exists 300
Adding the Event ID to the Query String 301
Remove Event Data from the Modal Window 302
Ensuring Only New Events Are Added to the Calendar 304
Confirming Deletion in a Modal Window 306
Displaying the Confirmation Dialog 306
Configuring the Form Submission Event Handler for Deletion 309
Modifying the Processing File to Confirm Deletion 310
Remove the Event from the Calendar After Deletion 312
Modifying the Form Submission Handler to Remove Deleted Events 313
Summary 315
PART 4 Advancing jQuery and PHP 316
CHAPTER 9 Performing Form Validation with Regular Expressions 317
Getting Comfortable with Regular Expressions 317
Understanding Basic Regular Expression Syntax 317
Setting up a Test File 318
Replacing Text with Regexes 319
Choosing Regexes vs. Regular String Replacement 320
Drilling Down on the Basics of Pattern Modifiers 322
Getting Fancy with Backreferences 324
Matching Character Classes 326
Matching Any Character Except... 328
Using Character Class Shorthand 329
Finding Word Boundaries 329
Using Repetition Operators 329
Detecting the Beginning or End of a String 330
Using Alternation 330
Using Optional Items 331
Putting It All Together 332
Adding Server-Side Date Validation 334
Defining the Regex Pattern to Validate Dates 334
Setting up Test Data 334
Matching the Date Format 336
Adding a Validation Method to the Calendar Class 339
Returning an Error if the Dates Don’t Validate 340
Adding Client-Side Date Validation 344
Creating a New JavaScript File to Validate the Date String 344
Including the New File in the Footer 345
Preventing the Form Submission if Validation Fails 345
Summary 349
CHAPTER 10 Extending jQuery 350
Adding Functions to jQuery 350
Adding Your Date Validation Function to jQuery 350
Allowing Custom Aliases in jQuery Plugins 350
Attaching the Function to the jQuery Object 351
Allowing Configurable Options 351
Extending Default Options with User-Supplied Options 352
Performing Validation and Returning a Value 353
Conforming to jQuery Plugin File Naming Conventions 353
Modifying the Include Script 353
Modifying the Initialization Script 354
Adding Methods to jQuery 356
Building Your Plugin 356
Creating a Properly Named Plugin File 356
Providing Publicly Accessible Default Options 357
Maintaining Chainability 358
Creating a Publicly Accessible Helper Method 359
Modifying Each Matched Element 361
Implementing Your Plugin 362
Including the Plugin File 362
Initializing the Plugin on a Set of Elements 363
Summary 365
Index 366
Erscheint lt. Verlag | 6.8.2010 |
---|---|
Zusatzinfo | 400 p. |
Verlagsort | Berkeley |
Sprache | englisch |
Themenwelt | Mathematik / Informatik ► Informatik ► Theorie / Studium |
Mathematik / Informatik ► Informatik ► Web / Internet | |
Schlagworte | AJAX • Animation • CSS • JavaScript • jQuery • JSON • PHP • Web Services • WINDOWS • XML |
ISBN-10 | 1-4302-2848-2 / 1430228482 |
ISBN-13 | 978-1-4302-2848-6 / 9781430228486 |
Haben Sie eine Frage zum Produkt? |
![PDF](/img/icon_pdf_big.jpg)
Größe: 12,7 MB
DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasserzeichen und ist damit für Sie personalisiert. Bei einer missbräuchlichen Weitergabe des eBooks an Dritte ist eine Rückverfolgung an die Quelle möglich.
Dateiformat: PDF (Portable Document Format)
Mit einem festen Seitenlayout eignet sich die PDF besonders für Fachbücher mit Spalten, Tabellen und Abbildungen. Eine PDF kann auf fast allen Geräten angezeigt werden, ist aber für kleine Displays (Smartphone, eReader) nur eingeschränkt geeignet.
Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.
Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.
aus dem Bereich