Phil Ballard
Michael Moncur

Sams Teach Yourself Ajax, JavaScript and PHP All in One

Image 800 East 96th Street, Indianapolis, Indiana, 46240 USA

Sams Teach Yourself Ajax, JavaScript, and PHP All in One

Copyright © 2009 by Sams Publishing

All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without 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 book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.

ISBN-13: 978-0-672-32965-4

ISBN-10: 0-672-32965-4

Library of Congress Cataloging-in-Publication Data
Ballard, Phil.
  Sams teach yourself Ajax, JavaScript, and PHP all in one / Phil
Ballard, Michael Moncur.
        p. cm.
Includes index.
ISBN 978-0-672-32965-4 (pbk. : CD-ROM)
1. Ajax (Web site development technology) 2. JavaScript (Computer program language)
3. PHP (Computer program language) 4. Web site development. I. Moncur, Michael G. II. Title. III. Title: Teach yourself Ajax, JavaScript, and PHP all in one.
  TK5105.8885.A52B38 2008
  006.7’6--dc22
                                                                                        2008022476

Printed in the United States of America
First Printing June 2008

Trademarks
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.

Warning and Disclaimer
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 authors and the publisher shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book or from the use of the CD or programs accompanying it.

Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales. For more information, please contact

U.S. Corporate and Government Sales
1-800-382-3419
[email protected]

For sales outside of the U.S., please contact

International Sales
[email protected]

Editor-in-Chief
Mark Taub

Acquisitions Editor
Mark Taber

Managing Editor
Patrick Kanouse

Project Editor
Mandie Frank

Indexer
Ken Johnson

Proofreader
Paula Lowell

Publishing Coordinator
Vanessa Evans

Multimedia Developer
Dan Scherf

Designer
Gary Adair

Composition
TnT Design, Inc.

Image

The Safari® Enabled icon on the cover of your favorite technology book means the book is available through Safari Bookshelf. When you buy this book, you get free access to the online edition for 45 days. Safari Bookshelf is an electronic reference library that lets you easily search thousands of technical books, find code samples, download chapters, and access technical information whenever and wherever you need it.

To gain 45-day Safari Enabled access to this book:

Image Go to http://www.informit.com/onlineedition

Image Complete the brief registration form

Image Enter the coupon code 37H1-TGKI-1KQV-LRIZ-VM1R

If you have difficulty registering on Safari Bookshelf or accessing the online edition, please email [email protected].

Contents at a Glance

Introduction

Part I: Web Basics Refresher

CHAPTER 1 Workings of the Web

2 Writing and Styling Pages in HTML and CSS

3 Anatomy of an Ajax Application

Part II: Introducing Web Scripting with JavaScript

CHAPTER 4 Creating Simple Scripts in JavaScript

5 Working with the Document Object Model (DOM)

6 Using Variables, Strings, and Arrays

7 Using Functions and Objects

8 Controlling Flow with Conditions and Loops

9 Using Built-In Functions and Libraries

Part III: Introducing Ajax

CHAPTER 10 The Heart of Ajax—the XMLHTTPRequest Object

11 Talking with the Server

12 Using the Returned Data

13 Our First Ajax Application

Part IV: Server-side Scripting with PHP

CHAPTER 14 Getting to Know PHP

15 Variables

16 Flow Control

17 Functions

18 Using Classes

Part V: More Complex Ajax Technologies

CHAPTER 19 Returning Data as Text

20 AHAH—Asynchronous HTML and HTTP

21 Returning Data as XML

22 Web Services and the REST and SOAP Protocols

23 A JavaScript Library for Ajax

24 Ajax Gotchas

Part VI: Ajax Tools and Resources

CHAPTER 25 The prototype.js Toolkit

26 Using Rico

27 Using Script.aculo.us

28 Using XOAD

APPENDIX JavaScript, PHP, and Ajax Websites

GLOSSARY

Index

Table of Contents

Introduction

Part I: Web Basics Refresher

CHAPTER 1: Workings of the Web

A Short History of the Internet

The World Wide Web

Introducing HTTP

The HTTP Request and Response

HTML Forms

Summary

CHAPTER 2: Writing and Styling Pages in HTML and CSS

Introducing HTML

Elements of an HTML Page

A More Advanced HTML Page

Some Useful HTML Tags

Adding Your Own Style

Defining the Rules

Add a Little class

Applying Styles

Formatting Text with Styles

Adding Lines

Summary

CHAPTER 3: Anatomy of an Ajax Application

The Need for Ajax

Introducing Ajax

The Constituent Parts of Ajax

Putting It All Together

Summary

Part II: Introducing Web Scripting with JavaScript

CHAPTER 4: Creating Simple Scripts in JavaScript

Tools for Scripting

Displaying Time with JavaScript

Beginning the Script

Adding JavaScript Statements

Creating Output

Adding the Script to a Web Page

Testing the Script

JavaScript Syntax Rules

Using Comments

Best Practices for JavaScript

Summary

CHAPTER 5: Working with the Document Object Model (DOM)

Understanding Objects

Understanding the Document Object Model (DOM)

Working with Web Documents

Accessing Browser History

Working with the location Object

Summary

CHAPTER 6: Using Variables, Strings, and Arrays

Using Variables

Understanding Expressions and Operators

Data Types in JavaScript

Converting Between Data Types

Using String Objects

Working with Substrings

Using Numeric Arrays

Using String Arrays

Sorting a Numeric Array

Summary

CHAPTER 7: Using Functions and Objects

Using Functions

Introducing Objects

Using Objects to Simplify Scripting

Extending Built-in Objects

Summary

CHAPTER 8: Controlling Flow with Conditions and Loops

The if Statement

Using Shorthand Conditional Expressions

Testing Multiple Conditions with if and else

Using Multiple Conditions with switch

Using for Loops

Using while Loops

Using do…while Loops

Working with Loops

Looping Through Object Properties

Summary

CHAPTER 9: Using Built-In Functions and Libraries

Using the Math Object

Working with Math Functions

Using the with Keyword

Working with Dates

Using Third-Party Libraries

Other Libraries

Summary

Part III: Introducing Ajax

CHAPTER 10: The Heart of Ajax—the XMLHTTPRequest Object

Introducing XMLHTTPRequest

Creating the XMLHTTPRequest Object

Summary

CHAPTER 11: Talking with the Server

Sending the Server Request

Monitoring Server Status

The Callback Function

Summary

CHAPTER 12: Using the Returned Data

The responseText and responseXML Properties

Parsing responseXML

Providing User Feedback

Summary

CHAPTER 13: Our First Ajax Application

Constructing the Ajax Application

The HTML Document

Adding JavaScript

Putting It All Together

Summary

Part IV: Server-side Scripting with PHP

CHAPTER 14: Getting to Know PHP

PHP Basics

Your First PHP Script

Summary

CHAPTER 15: Variables

Understanding Variables

Data Types

Working with Numbers

Numeric Data Types

Numeric Functions

Working with Strings

Formatting Strings

String Functions

Working with Arrays

Array Functions

Date Formats

Working with Timestamps

Summary

CHAPTER 16: Flow Control

Conditional Statements

Loops

Summary

CHAPTER 17: Functions

Using Functions

Arguments and Return Values

Using Library Files

Summary

CHAPTER 18: Using Classes

Object-Oriented PHP

What Is a Class?

Creating and Using Objects

Summary

Part V: More Complex Ajax Technologies

CHAPTER 19: Returning Data as Text

Getting More from the responseText Property

Summary

CHAPTER 20: AHAH—Asynchronous HTML and HTTP

Introducing AHAH

Creating a Small Library for AHAH

Using myAHAHlib.js

Summary

CHAPTER 21: Returning Data as XML

Adding the “x” to Ajax

The responseXML Property

Project—An RSS Headline Reader

Summary

CHAPTER 22: Web Services and the REST and SOAP Protocols

Introduction to Web Services

REST—Representational State Transfer

Using REST in Practice

Web Services Using SOAP

The SOAP Protocol

Using Ajax and SOAP

Reviewing SOAP and REST

Summary

CHAPTER 23: A JavaScript Library for Ajax

An Ajax Library

Reviewing myAHAHlib.js

Implementing Our Library

Using the Library

Summary

CHAPTER 24: Ajax Gotchas

Common Ajax Errors

The Back Button

Bookmarking and Links

Telling the User That Something Is Happening

Making Ajax Degrade Elegantly

Dealing with Search Engine Spiders

Pointing Out Active Page Elements

Don’t Use Ajax Where It’s Inappropriate

Security

Test Code Across Multiple Platforms

Ajax Won’t Cure a Bad Design

Some Programming Gotchas

Summary

Part VI: Ajax Tools and Resources

CHAPTER 25: The prototype.js Toolkit

Introducing prototype.js

Wrapping XMLHTTPRequest—the Ajax Object

Example Project—Stock Price Reader

Summary

CHAPTER 26: Using Rico

Introducing Rico

Rico’s Other Interface Tools

Summary

CHAPTER 27: Using Script.aculo.us

Downloading the Library

Including the Files

Using Effects

Building the Script

Summary

CHAPTER 28: Using XOAD

Introducing XOAD

XOAD HTML

Advanced Programming with XOAD

Summary

APPENDIX: JavaScript, PHP, and Ajax Websites

JavaScript Websites

PHP Websites

Web Development Sites

Ajax Websites

GLOSSARY

Index

About the Author

Phil Ballard, the author of Sams Teach Yourself Ajax in 10 Minutes, graduated in 1980 with an honors degree in electronics from the University of Leeds, England. Following an early career as a research scientist with a major multinational, he spent a few years in commercial and managerial roles within the high technology sector, later working full time as a software engineering consultant.

Operating as “The Mouse Whisperer” (http://www.mousewhisperer.co.uk), Ballard has spent recent years involved solely in website and intranet design and development for an international portfolio of clients.

Michael Moncur is a freelance webmaster and author. He runs a network of websites, including the Web’s oldest site about famous quotations, online since 1994. He wrote Sams Teach Yourself JavaScript in 24 Hours and has also written several bestselling books about networking, certification programs, and databases. He lives with his wife in Salt Lake City, Utah.

We Want to Hear from You!

As the reader of this book, you are our most important critic and commentator. We value your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass our way.

You can email or write me directly to let me know what you did or didn’t like about this book—as well as what we can do to make our books stronger.

Please note that I cannot help you with technical problems related to the topic of this book, and that due to the high volume of mail I receive, I might not be able to reply to every message.

When you write, please be sure to include this book’s title and author as well as your name and phone or email address. I will carefully review your comments and share them with the author and editors who worked on the book.

E-mail:

[email protected]

Mail:

Mark Taub
Associate Publisher
Sams Publishing
800 East 96th Street
Indianapolis, IN 46240 USA

Reader Services

Visit our website and register this book at informit.com/register for convenient access to any updates, downloads, or errata that might be available for this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.105.15