27.10.2015 Views

AJAX and PHP

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<br />

<br />

<br />

<br />

<br />

This example requires a JavaScript-enabled browser!<br />

<br />

<br />

<br />

Welcome to <strong>AJAX</strong> Friendly!<br />

<br />

<br />

Your news for today:<br />

<br />

<br />

<br />

<br />

<br />

4. Add a new file named friendly.css:<br />

body<br />

{<br />

font-family: Arial, Helvetica, sans-serif;<br />

font-size: small;<br />

background-color: #fffccc;<br />

}<br />

input<br />

{<br />

margin-bottom: 3px;<br />

border: #000099 1px solid;<br />

}<br />

.title<br />

{<br />

font-size: x-large;<br />

}<br />

div.project<br />

{<br />

background-color: #99ccff;<br />

padding: 5px;<br />

border: #000099 1px solid;<br />

}<br />

div.news<br />

{<br />

background-color: #fffbb8;<br />

padding: 2px;<br />

border: 1px dashed;<br />

}<br />

5. Now add the JavaScript source file, friendly.js:<br />

// holds an instance of XMLHttpRequest<br />

var xmlHttp = createXmlHttpRequestObject();<br />

// holds the remote server address <strong>and</strong> parameters<br />

var serverAddress = "friendly.php?action=GetNews";<br />

// variables that establish how often to access the server<br />

var updateInterval = 5; // how many seconds to wait to get new message<br />

var errorRetryInterval = 30; // seconds to wait after server error<br />

// when set to true, display detailed error messages<br />

var debugMode = true;<br />

Chapter 3<br />

113<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!