Table Of ContentJulie C. Meloni
SamsTeachYourself
PHP, MySQL
& JavaScript
All
One
in
SIXTH EDITION
Sams Teach Yourself PHP, MySQL & JavaScript All in One, Sixth Edition Editor
Copyright © 2018 by Pearson Education, Inc. Mark Taber
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or
Managing Editor
transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without
Sandra Schroeder
written permission from the publisher. No patent liability is assumed with respect to the use of the
information contained herein. Although every precaution has been taken in the preparation of this Project Editor
book, the publisher and author assume no responsibility for errors or omissions. Nor is any liabil-
Mandie Frank
ity assumed for damages resulting from the use of the information contained herein.
ISBN-13: 978-0-672-33770-3 Copy Editor
ISBN-10: 0-672-33770-3 Bart Reed
Library of Congress Control Number: 2017911865
Indexer
Printed in the United States of America
Erika Millen
01 17
Trademarks Proofreader
Abby Manheim
All terms mentioned in this book that are known to be trademarks or service marks have been
appropriately capitalized. Pearson cannot attest to the accuracy of this information. Use of a term Technical Editor
in this book should not be regarded as affecting the validity of any trademark or service mark.
Timothy Boronczyk
Warning and Disclaimer
Editorial Assistant
Every effort has been made to make this book as complete and as accurate as possible, but no
warranty or fitness is implied. The information provided is on an “as is” basis. The author and the Vanessa Evans
publisher shall have neither liability nor responsibility to any person or entity with respect to any
Designer
loss or damages arising from the information contained in this book or from the use of the CD or
Chuti Prasertsith
programs accompanying it.
Special Sales Compositor
For information about buying this title in bulk quantities, or for special sales opportunities (which codeMantra
may include electronic versions; custom cover designs; and content particular to your business,
training goals, marketing focus, or branding interests), please contact our corporate sales depart-
ment at corpsales@pearsoned.com or (800) 382-3419.
For government sales inquiries, please contact governmentsales@pearsoned.com.
For questions about sales outside the U.S., please contact intlcs@pearson.com.
Contents at a Glance
Introduction .................................................................................1
Part I: Web Application Basics
CHAPTER 1 Understanding How the Web Works ...............................................5
2 Structuring HTML and Using Cascading Style Sheets ...................... 25
3 Understanding the CSS Box Model and Positioning ....................... 75
4 Introducing JavaScript .............................................................. 107
5 Introducing PHP ....................................................................... 127
Part II: Getting Started with Dynamic Websites
CHAPTER 6 Understanding Dynamic Websites and HTML5 Applications ......... 139
7 JavaScript Fundamentals: Variables, Strings, and Arrays .............. 175
8 JavaScript Fundamentals: Functions, Objects, and Flow Control .... 197
9 Understanding JavaScript Event Handling .................................. 239
10 The Basics of Using jQuery ........................................................ 261
Part III: Taking Your Web Applications to the Next Level
CHAPTER 11 AJAX: Remote Scripting ............................................................. 279
12 PHP Fundamentals: Variables, Strings, and Arrays ....................... 299
13 PHP Fundamentals: Functions, Objects, and Flow Control ............ 327
14 Working with Cookies and User Sessions ..................................... 367
15 Working with Web-Based Forms ................................................. 385
Part IV: Integrating a Database into Your Applications
CHAPTER 16 Understanding the Database Design Process ............................... 425
17 Learning Basic SQL Commands ................................................. 437
18 Interacting with MySQL Using PHP ............................................ 487
iv Sams Teach Yourself PHP, MySQL & JavaScript All in One, Sixth Edition
Part V: Getting Started with Application Development
CHAPTER 19 Creating a Simple Discussion Forum .......................................... 501
20 Creating an Online Storefront .................................................... 523
21 Creating a Simple Calendar ...................................................... 541
22 Managing Web Applications ..................................................... 567
Appendixes
A Installation QuickStart Guide with XAMPP ................................. 583
B Installing and Configuring MySQL ............................................. 597
C Installing and Configuring Apache ............................................ 617
D Installing and Configuring PHP ................................................. 635
Index ....................................................................................... 645
Table of Contents
Introduction 1
Part I: Web Application Basics
CHAPTER 1: Understanding How the Web Works 5
A Brief History of HTML and the World Wide Web ........................................6
Creating Web Content ................................................................................7
Understanding Web Content Delivery ..........................................................7
Selecting a Web Hosting Provider .............................................................. 10
Testing with Multiple Web Browsers ........................................................... 12
Creating a Sample File ............................................................................. 13
Using FTP to Transfer Files ........................................................................ 14
Understanding Where to Place Files on the Web Server ................................ 19
Summary ................................................................................................ 22
Q&A ....................................................................................................... 22
Workshop ................................................................................................ 23
CHAPTER 2: Structuring HTML and Using Cascading Style Sheets 25
Getting Started with a Simple Web Page .................................................... 26
HTML Tags Every Web Page Must Have...................................................... 29
Using Hyperlinks in Web Pages ................................................................. 31
Organizing a Page with Paragraphs and Line Breaks .................................. 33
Organizing Your Content with Headings .................................................... 36
Understanding Semantic Elements ............................................................. 39
How CSS Works ........................................................................................ 51
A Basic Style Sheet ................................................................................... 54
A CSS Style Primer.................................................................................... 59
Using Style Classes ................................................................................... 64
Using Style IDs ......................................................................................... 66
Internal Style Sheets and Inline Styles ........................................................ 66
vi Sams Teach Yourself PHP, MySQL & JavaScript All in One, Sixth Edition
Summary ................................................................................................ 69
Q&A ....................................................................................................... 71
Workshop ................................................................................................ 72
CHAPTER 3: Understanding the CSS Box Model and Positioning 75
The CSS Box Model .................................................................................. 76
The Whole Scoop on Positioning ............................................................... 79
Controlling the Way Things Stack Up ........................................................ 84
Managing the Flow of Text ....................................................................... 86
Understanding Fixed Layouts .................................................................... 87
Understanding Fluid Layouts .................................................................... 89
Creating a Fixed/Fluid Hybrid Layout ........................................................ 92
Considering a Responsive Web Design ..................................................... 103
Summary .............................................................................................. 104
Q&A ..................................................................................................... 105
Workshop .............................................................................................. 105
CHAPTER 4: Introducing JavaScript 107
Learning Web Scripting Basics ................................................................. 108
How JavaScript Fits into a Web Page ........................................................ 109
Exploring JavaScript’s Capabilities ........................................................... 111
Basic JavaScript Language Concepts ........................................................ 112
JavaScript Syntax Rules .......................................................................... 118
Using Comments.................................................................................... 119
Best Practices for JavaScript ..................................................................... 120
Understanding JSON .............................................................................. 121
Using the JavaScript Console to Debug JavaScript ..................................... 122
Summary .............................................................................................. 124
Q&A ..................................................................................................... 124
Workshop .............................................................................................. 125
CHAPTER 5: Introducing PHP 127
How PHP Works with a Web Server .......................................................... 127
The Basics of PHP Scripts ........................................................................ 128
Code Blocks and Browser Output ............................................................. 134
Table of Contents vii
Summary .............................................................................................. 136
Q&A ..................................................................................................... 137
Workshop .............................................................................................. 137
Part II: Getting Started with Dynamic Websites
CHAPTER 6: Understanding Dynamic Websites and HTML5 Applications 139
Refresher on the Different Types of Scripting ............................................. 140
Displaying Random Content on the Client Side ........................................ 140
Understanding the Document Object Model ............................................. 144
Using window Objects ............................................................................. 145
Working with the document Object ......................................................... 146
Accessing Browser History ....................................................................... 149
Working with the location Object ......................................................... 151
More About the DOM Structure ............................................................... 152
Working with DOM Nodes ...................................................................... 155
Creating Positionable Elements (Layers) ................................................... 157
Hiding and Showing Objects ................................................................... 162
Modifying Text Within a Page ................................................................. 164
Adding Text to a Page ............................................................................ 166
Changing Images Based on User Interaction ............................................ 168
Thinking Ahead to Developing HTML5 Applications ................................. 170
Summary .............................................................................................. 171
Q&A ..................................................................................................... 172
Workshop .............................................................................................. 172
CHAPTER 7: JavaScript Fundamentals: Variables, Strings, and Arrays 175
Using Variables ...................................................................................... 175
Understanding Expressions and Operators ............................................... 179
Data Types in JavaScript ......................................................................... 180
Converting Between Data Types .............................................................. 181
Using String Objects ............................................................................. 182
Working with Substrings ......................................................................... 185
Using Numeric Arrays ............................................................................ 188
Using String Arrays ................................................................................ 189
Sorting a Numeric Array ......................................................................... 191
viii Sams Teach Yourself PHP, MySQL & JavaScript All in One, Sixth Edition
Summary .............................................................................................. 194
Q&A ..................................................................................................... 195
Workshop .............................................................................................. 195
CHAPTER 8: JavaScript Fundamentals: Functions, Objects, and Flow Control 197
Using Functions ..................................................................................... 198
Introducing Objects ................................................................................ 203
Using Objects to Simplify Scripting .......................................................... 205
Extending Built-in Objects ...................................................................... 210
Using the Math Object ............................................................................ 212
Working with Math Methods ................................................................... 213
Working with Dates ................................................................................ 216
The if Statement ................................................................................... 219
Using Shorthand Conditional Expressions ................................................ 222
Testing Multiple Conditions with if and else ......................................... 223
Using Multiple Conditions with switch ................................................... 225
Using for Loops .................................................................................... 227
Using while Loops ................................................................................. 229
Using do…while Loops ........................................................................... 230
Working with Loops ............................................................................... 231
Looping Through Object Properties .......................................................... 232
Summary .............................................................................................. 236
Q&A ..................................................................................................... 236
Workshop .............................................................................................. 237
CHAPTER 9: Understanding JavaScript Event Handling 239
Understanding Event Handlers ................................................................ 239
Using Mouse Events ................................................................................ 244
Using Keyboard Events ........................................................................... 248
Using the load and unload Events ......................................................... 250
Using click to Change a <div>'s Appearance ....................................... 251
Summary .............................................................................................. 258
Q&A ..................................................................................................... 258
Workshop .............................................................................................. 258
Table of Contents ix
CHAPTER 10: The Basics of Using jQuery 261
Using Third-Party JavaScript Libraries ...................................................... 261
jQuery Arrives on the Scene .................................................................... 263
Preparing to Use jQuery .......................................................................... 264
Becoming Familiar with the $().ready Handler ..................................................................265
Selecting DOM and CSS Content ............................................................. 267
Manipulating HTML Content .................................................................. 267
Putting the Pieces Together to Create a jQuery Animation ......................... 272
Handling Events with jQuery .................................................................. 275
Summary .............................................................................................. 276
Q&A ..................................................................................................... 277
Workshop .............................................................................................. 277
Part III: Taking Your Web Applications to the Next Level
CHAPTER 11: AJAX: Remote Scripting 279
Introducing AJAX ................................................................................... 279
Using XMLHttpRequest ......................................................................... 282
Creating a Simple AJAX Library .............................................................. 283
Creating an AJAX Quiz Using the Library ................................................ 285
Debugging AJAX-Based Applications ....................................................... 290
Using jQuery’s Built-in Functions for AJAX ............................................... 295
Summary .............................................................................................. 296
Q&A ..................................................................................................... 296
Workshop .............................................................................................. 296
CHAPTER 12: PHP Fundamentals: Variables, Strings, and Arrays 299
Variables ............................................................................................... 299
Data Types ............................................................................................ 302
Using Expressions and Operators ............................................................. 307
Constants .............................................................................................. 315
Understanding Arrays ............................................................................ 317
Creating Arrays ..................................................................................... 318
Some Array-Related Constructs and Functions .......................................... 322