Sams Teach Yourself Ajax, JavaScript, and PHP All in One [NOOK Book]

Overview

In just a short time, you can learn how to use Ajax, JavaScript, and PHP to create interactive interfaces to your web applications by combining these powerful technologies.

No previous Ajax programming experience is required. Using a straightforward, step-by-step approach, each lesson in this book builds on the previous ones, enabling you to learn the essentials of Ajax programming with JavaScript, PHP, and related technologies from the ground ...

See more details below
Sams Teach Yourself Ajax, JavaScript, and PHP All in One

Available on NOOK devices and apps  
  • NOOK Devices
  • Samsung Galaxy Tab 4 NOOK
  • NOOK HD/HD+ Tablet
  • NOOK
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac

Want a NOOK? Explore Now

NOOK Book (eBook)
$17.99
BN.com price
(Save 43%)$31.99 List Price

Overview

In just a short time, you can learn how to use Ajax, JavaScript, and PHP to create interactive interfaces to your web applications by combining these powerful technologies.

No previous Ajax programming experience is required. Using a straightforward, step-by-step approach, each lesson in this book builds on the previous ones, enabling you to learn the essentials of Ajax programming with JavaScript, PHP, and related technologies from the ground up.

Regardless of whether you run Linux, Windows, or Mac OS X, the enclosed CD includes a complete Ajax programming starter kit that gives you all the programming tools, reference information, JavaScript libraries, and server software you need to set up a stable environment for learning, testing, and production.

Learn how to…

  • Build better, more interactive interfaces for your web applications
  • Make JavaScript, HTML, XML, and PHP work together to create Ajax effects
  • Compile an Ajax application
  • Create and consume web services with SOAP and REST
  • Avoid common errors and troubleshoot programs
  • Use popular Ajax libraries to speed up and improve common programming tasks

On the CD

  • XAMPP for Windows, Mac OS X, and Linux—an easy-to-install package to set up a PHP- and MySQL-enabled Apache server on your computer
  • The jEdit programming editor for Windows, Mac, and Linux
  • Prototype, Scriptaculous, Rico, and XOAD—popular JavaScript libraries for creating Ajax applications and effects
  • A complete Ajax, HTML, XML, and PHP tutorial reference library in searchable PDF format
  • Source code for the examples in the book

Phil Ballard is a software engineering consultant and developer specializing in website and intranet design and development for an international portfolio of clients. He has an honors degree from the University of Leeds, England, and has worked for several years in commercial and managerial roles in the high technology sector.

Michael Moncur is a freelance webmaster and author. He runs a network of websites and has written several bestselling books about web development, networking, certification programs, and databases.

Category: Web Development

Covers: Ajax, JavaScript and PHP

User Level: Beginning–Intermediate

Read More Show Less

Product Details

  • ISBN-13: 9780768685442
  • Publisher: Pearson Education
  • Publication date: 7/16/2008
  • Series: Sams Teach Yourself Series
  • Sold by: Barnes & Noble
  • Format: eBook
  • Edition number: 1
  • Pages: 384
  • File size: 4 MB

Meet 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” (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.

Read More Show Less

Table of Contents

Introduction

Part I: Web Basics Refresher 7

Chapter 1: Workings of the Web 9

A Short History of the Internet........................................................... 9

The World Wide Web..................................................................... 10

Introducing HTTP............................................................................ 14

The HTTP Request and Response................................................... 15

HTML Forms.................................................................................. 17

Summary......................................................................................... 20

Chapter 2: Writing and Styling Pages in HTML and CSS 21

Introducing HTML.......................................................................... 21

Elements of an HTML Page............................................................. 23

A More Advanced HTML Page...................................................... 27

Some Useful HTML Tags................................................................ 29

Adding Your Own Style.................................................................. 30

Defining the Rules............................................................................ 31

Add a Little class............................................................................. 31

Applying Styles................................................................................ 33

Formatting Text with Styles.............................................................. 35

Adding Lines................................................................................... 39

Summary......................................................................................... 40

Chapter 3: Anatomy of an Ajax Application 41

The Need for Ajax.......................................................................... 41

Introducing Ajax.............................................................................. 43

The Constituent Parts of Ajax.......................................................... 45

Putting It All Together...................................................................... 47

Summary......................................................................................... 49

Part II: Introducing Web Scripting with JavaScript 51

Chapter 4: Creating Simple Scripts in JavaScript 53

Tools for Scripting........................................................................... 53

Displaying Time with JavaScript....................................................... 54

Beginning the Script......................................................................... 55

Adding JavaScript Statements.......................................................... 55

Creating Output............................................................................... 56

Adding the Script to a Web Page..................................................... 57

Testing the Script............................................................................. 58

JavaScript Syntax Rules................................................................... 65

Using Comments............................................................................. 66

Best Practices for JavaScript............................................................ 67

Summary......................................................................................... 69

Chapter 5: Working with the Document Object Model (DOM) 71

Understanding Objects.................................................................... 71

Understanding the Document Object Model (DOM)........................ 72

Working with Web Documents........................................................ 74

Accessing Browser History.............................................................. 77

Working with the location Object..................................................... 78

Summary......................................................................................... 80

Chapter 6: Using Variables, Strings, and Arrays 81

Using Variables............................................................................... 81

Understanding Expressions and Operators....................................... 85

Data Types in JavaScript................................................................. 86

Converting Between Data Types...................................................... 88

Using String Objects........................................................................ 89

Working with Substrings.................................................................. 92

Using Numeric Arrays..................................................................... 94

Using String Arrays.......................................................................... 96

Sorting a Numeric Array.................................................................. 98

Summary....................................................................................... 101

chapter 7: Using Functions and Objects 103

Using Functions............................................................................. 103

Introducing Objects....................................................................... 108

Using Objects to Simplify Scripting................................................ 109

Extending Built-in Objects.............................................................. 112

Summary....................................................................................... 115

Chapter 8: Controlling Flow with Conditions and Loops 117

The if Statement............................................................................. 118

Using Shorthand Conditional Expressions....................................... 121

Testing Multiple Conditions with if and else..................................... 122

Using Multiple Conditions with switch............................................ 124

Using for Loops............................................................................. 125

Using while Loops......................................................................... 128

Using do…while Loops................................................................. 128

Working with Loops...................................................................... 129

Looping Through Object Properties............................................... 131

Summary....................................................................................... 134

Chapter 9: Using Built-In Functions and Libraries 135

Using the Math Object................................................................... 135

Working with Math Functions........................................................ 137

Using the with Keyword................................................................ 139

Working with Dates....................................................................... 140

Using Third-Party Libraries............................................................ 143

Other Libraries.............................................................................. 144

Summary....................................................................................... 145

Part III: Introducing Ajax 147

Chapter 10: The Heart of Ajax—the XMLHTTPRequest Object 149

Introducing XMLHTTPRequest..................................................... 149

Creating the XMLHTTPRequest Object........................................ 150

Summary....................................................................................... 156

Chapter 11: Talking with the Server 157

Sending the Server Request........................................................... 157

Monitoring Server Status............................................................... 162

The Callback Function................................................................... 163

Summary....................................................................................... 166

Chapter 12: Using the Returned Data 167

The responseText and responseXML Properties............................ 167

Parsing responseXML................................................................... 171

Providing User Feedback.............................................................. 172

Summary....................................................................................... 174

Chapter 13: Our First Ajax Application 175

Constructing the Ajax Application.................................................. 175

The HTML Document................................................................... 176

Adding JavaScript......................................................................... 177

Putting It All Together.................................................................... 180

Summary....................................................................................... 184

Part IV: Server-side Scripting with PHP 185

Chapter 14: Getting to Know PHP 187

PHP Basics................................................................................... 187

Your First PHP Script................................................................... 190

Summary....................................................................................... 194

Chapter 15: Variables 195

Understanding Variables................................................................ 195

Data Types.................................................................................... 197

Working with Numbers.................................................................. 198

Numeric Data Types...................................................................... 200

Numeric Functions......................................................................... 200

Working with Strings..................................................................... 202

Formatting Strings.......................................................................... 203

String Functions............................................................................. 205

Working with Arrays..................................................................... 206

Array Functions............................................................................. 208

Date Formats................................................................................ 209

Working with Timestamps.............................................................. 210

Summary....................................................................................... 213

Chapter 16: Flow Control 215

Conditional Statements.................................................................. 215

Loops........................................................................................... 220

Summary....................................................................................... 222

Chapter 17: Functions 223

Using Functions............................................................................. 223

Arguments and Return Values........................................................ 225

Using Library Files......................................................................... 229

Summary....................................................................................... 229

Chapter 18: Using Classes 231

Object-Oriented PHP.................................................................... 231

What Is a Class?........................................................................... 232

Creating and Using Objects........................................................... 233

Summary....................................................................................... 236

Part V: More Complex Ajax Technologies 237

Chapter 19: Returning Data as Text 239

Getting More from the responseText Property................................ 239

Summary....................................................................................... 245

Chapter 20: AHAH—Asynchronous HTML and HTTP 247

Introducing AHAH........................................................................ 247

Creating a Small Library for AHAH............................................... 248

Using myAHAHlib.js..................................................................... 250

Summary....................................................................................... 255

Chapter 21: Returning Data as XML 257

Adding the “x” to Ajax................................................................... 257

The responseXML Property.......................................................... 258

Project—An RSS Headline Reader............................................... 262

Summary....................................................................................... 269

Chapter 22: Web Services and the REST and SOAP Protocols 271

Introduction to Web Services......................................................... 271

REST—Representational State Transfer......................................... 272

Using REST in Practice.................................................................. 274

Web Services Using SOAP........................................................... 278

The SOAP Protocol...................................................................... 279

Using Ajax and SOAP................................................................... 282

Reviewing SOAP and REST.......................................................... 283

Summary....................................................................................... 284

Chapter 23: A JavaScript Library for Ajax 285

An Ajax Library............................................................................ 285

Reviewing myAHAHlib.js.............................................................. 286

Implementing Our Library.............................................................. 287

Using the Library........................................................................... 290

Summary....................................................................................... 293

Chapter 24: Ajax Gotchas 295

Common Ajax Errors.................................................................... 295

The Back Button........................................................................... 296

Bookmarking and Links................................................................. 297

Telling the User That Something Is Happening................................ 297

Making Ajax Degrade Elegantly..................................................... 297

Dealing with Search Engine Spiders................................................ 298

Pointing Out Active Page Elements................................................. 299

Don’t Use Ajax Where It’s Inappropriate...................................... 299

Security......................................................................................... 300

Test Code Across Multiple Platforms............................................. 300

Ajax Won’t Cure a Bad Design..................................................... 301

Some Programming Gotchas.......................................................... 301

Summary....................................................................................... 302

Part VI: Ajax Tools and Resources 303

Chapter 25: The prototype.js Toolkit 305

Introducing prototype.js................................................................. 305

Wrapping XMLHTTPRequest—the Ajax Object........................... 308

Example Project—Stock Price Reader.......................................... 311

Summary....................................................................................... 313

Chapter 26: Using Rico 315

Introducing Rico............................................................................ 315

Rico’s Other Interface Tools.......................................................... 320

Summary....................................................................................... 324

Chapter 27: Using Script.aculo.us 325

Downloading the Library................................................................ 325

Including the Files.......................................................................... 326

Using Effects................................................................................. 326

Building the Script.......................................................................... 327

Summary....................................................................................... 329

Chapter 28: Using XOAD 331

Introducing XOAD........................................................................ 331

XOAD HTML.............................................................................. 334

Advanced Programming with XOAD............................................. 337

Summary....................................................................................... 338

Appendix: JavaScript, PHP, and Ajax Websites 339

Glossary 343

Read More Show Less

Preface

Introduction

Over the last decade or so, the World Wide Web has grown in scope from being a relatively simple information repository to becoming the first stop for many people when seeking entertainment, education, news, or business resources.

Websites themselves need no longer be limited to a number of static pages containing text and perhaps simple images; the tools now available allow the development of highly interactive and engaging pages involving animations, visual effects, context-sensitive content, embedded productivity tools, and much more.

The list of technologies available for producing such pages is broad. However, those based on Open Source licenses have become, and remain, highly popular due to their typically low (often zero) entry cost, and to the huge resource of user-contributed scripts, tutorials, tools, and other resources for these tools and applications available via the Internet and elsewhere.

In this book, we give a detailed account of how to program fluid, interactive websites using server- and client-side coding techniques and tools, as well as how to combine these to produce a slick, desktop-application-like user experience using Ajax.

The programming languages used in this book include the ubiquitous JavaScript (for client-side programming) and the immensely popular open-source PHP language (for server-side scripting, and available with the majority of web-hosting packages). The nuts and bolts of Ajax programming are described in detail, as well as the use of several advanced open-source frameworks that contain ready-written code for quickly building state-of-the-art interactive sites.


Note - The CD that accompanies this book provides all the tools required on your journey through learning to program in PHP, JavaScript, and Ajax.


What Is Ajax?

Ajax stands for Asynchronous JavaScript And XML. Although strictly speaking Ajax is not itself a technology, it mixes well-known programming techniques in an uncommon way to enable web developers to build Internet applications with much more appealing user interfaces than those to which we have become accustomed.

When using popular desktop applications, we expect the results of our work to be made available immediately, without fuss, and without our having to wait for the whole screen to be redrawn by the program. While using a spreadsheet such as Excel, for instance, we expect the changes we make in one cell to propagate immediately through the neighboring cells while we continue to type, scroll the page, or use the mouse.

Unfortunately, this sort of interaction has seldom been available to users of web-based applications. Much more common is the experience of entering data into form fields, clicking on a button or a hyperlink and then sitting back while the page slowly reloads to exhibit the results of the request. In addition, we often find that the majority of the reloaded page consists of elements that are identical to those of the previous page and that have therefore been reloaded unnecessarily; background images, logos, and menus are frequent offenders.

Ajax promises us a solution to this problem. By working as an extra layer between the user's browser and the web server, Ajax handles server communications in the background, submitting server requests and processing the returned data. The results may then be integrated seamlessly into the page being viewed, without that page needing to be refreshed or a new one being loaded.

In Ajax applications, such server requests are not necessarily synchronized with user actions such as clicking on buttons or links. A well-written Ajax application may already have asked of the server, and received, the data required by the user—perhaps before the user even knew she wanted it. This is the meaning of the asynchronous part of the Ajax acronym.

The parts of an Ajax application that happen "under the hood" of the user's browser, such as sending server queries and dealing with the returned data, are written in JavaScript, and XML is an increasingly popular means of coding and transferring formatted information used by Ajax to efficiently transfer data between server and client.

We'll look at all these techniques, and how they can be made to work together, as we work through the chapters.

Who This Book Is For

This volume is aimed primarily at web developers seeking to build better interfaces for the users of their web applications and programmers from desktop environments looking to transfer their applications to the Internet.

It also proves useful to web designers eager to learn how the latest techniques can offer new outlets for their creativity. Although the nature of PHP, JavaScript, and Ajax applications means that they require some programming, all the required technologies are explained from first principles within the book, so even those with little or no programming experience should be able to follow the lessons without a great deal of difficulty.

How To Use This Book

All the technologies—including a refresher of WWW basics—are explained from first principles, so that even non-programmers or those unfamiliar with these languages should be able to follow the development of the concepts with little problem.

The book is divided into parts, each dedicated to a particular technology or discussion topic. Within each part, the chapters each specialize in a given aspect or subtopic. It should therefore be easy to follow the instructional flow of the book by a quick look through the table of contents.

However, if you are already a competent programmer in one or more of the technologies used—in PHP for instance, or in JavaScript—then feel free to speed-read or skip the sections that you don't need.

To try out many of the examples you'll need access to a web server that supports PHP, and a means to upload files into your web space (probably FTP). Most web hosts include PHP in their hosting packages, or can do so on request at minimal or no cost.

Alternatively, the CD that accompanies this book contains everything required to set up a web serving environment on your own computer. This package is called XAMPP, and it contains everything you need to develop fully functional, interactive websites like those described in this book, ready to be deployed to a web-based server at a later date if you so choose. Look out for the boxes marked "On the CD" as you work through the book.

Conventions Used In This Book

This book contains special elements as described by the following:


Tip - These boxes highlight information that can make your programming more efficient and effective.



Note - These boxes provide additional information related to material you just read.



Caution - These boxes focus your attention on problems or side effects that can occur in specific situations.


Try It Yourself

The Try It Yourself section offers suggestions for creating your own scripts, experimenting further, or applying the techniques learned throughout the chapter. This will help you create practical applications based on what you've learned.


Note - Sections like this remind you about relevant information or tools available on the CD that accompanies the book.


A special monospace font is used on programming-related terms and language.

Setting Up Your Workspace

While you can write the code in this book using just a simple text editor, to run the examples you'll need a computer (with Windows, Mac, or Linux operating system) running a modern browser such as Internet Explorer or Firefox.


Tip - You can download Microsoft Windows Explorer from http://www.microsoft.com/ and the latest version of Firefox from http://www.mozilla.com/.


You will also need to load files on to a web server—if you already have a web host that supports PHP, you can use your web space there. Alternatively, the accompanying CD has everything you need to set up your own web server for private use, either on your own PC or another on your network.

What's on the CD

The accompanying CD contains everything you could need to get the best from this book. Included on the CD you'll find

  • XAMPP, a complete open source compilation you can use to easily install the Apache web server, PHP language, and MySQL database manager on your computer. Versions are provided for Linux, Mac, and Windows environments.
  • jEdit, a Java-based programmer's editor that's perfect for creating or modifying code. The CD includes files for Java, Mac, or Windows.
  • A selection of open source frameworks for developing sophisticated web applications. Programming examples based on some of these frameworks are presented towards the end of the book.

© Copyright Pearson Education. All rights reserved.

Read More Show Less

Customer Reviews

Average Rating 4
( 4 )
Rating Distribution

5 Star

(2)

4 Star

(1)

3 Star

(0)

2 Star

(1)

1 Star

(0)

Your Rating:

Your Name: Create a Pen Name or

Barnes & Noble.com Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & Noble.com that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & Noble.com does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at BN.com or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation

Reminder:

  • - By submitting a review, you grant to Barnes & Noble.com and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Noble.com Terms of Use.
  • - Barnes & Noble.com reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & Noble.com also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on BN.com. It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

 
Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously
Sort by: Showing all of 4 Customer Reviews
  • Anonymous

    Posted September 16, 2009

    No text was provided for this review.

  • Anonymous

    Posted April 21, 2009

    No text was provided for this review.

  • Anonymous

    Posted June 2, 2009

    No text was provided for this review.

  • Anonymous

    Posted October 24, 2010

    No text was provided for this review.

Sort by: Showing all of 4 Customer Reviews

If you find inappropriate content, please report it to Barnes & Noble
Why is this product inappropriate?
Comments (optional)