Chapter 39. Integrating PHP and JavaScript

In this chapter, we try to get the best of both client-side and server-side scripting by combining PHP with JavaScript. We briefly touch on the question of when to use which scripting language, and stylistic points that may be helpful when writing the code. Then we move on to pragmatic examples of the type you might see on a real-world PHP site.

Tip

If you've never worked with JavaScript before, you won't learn how just by reading this chapter. We will only touch on aspects of JavaScript that materially impinge upon PHP. If you're wondering what an onBlur event is, we recommend Danny Goodman's superlative JavaScript Bible, Sixth Edition (Wiley, 2007) or Steve Suehring's JavaScript Step by Step (Microsoft, 2008).

Outputting JavaScript with PHP

Because PHP is server-side and JavaScript is client-side, you may expect to have problems using both on the same page. In actuality, it's this difference that makes them such a good match.

Although PHP offers plenty of power for creating dynamically generated web pages, it is strictly a server-side language. There's a common category of web site tasks that perhaps don't require all the processing power of a server and would best be done quickly — for instance, changing the look of a button on mouseover. JavaScript, a purely client-side language (there's a server-side version, but ...

Get PHP6 and MySQL® 6 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.