13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

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

Adding Ajax Elements to Earlier Projects<br />

871<br />

changes you make will then be uploaded into this new directory rather than a working<br />

version of <strong>PHP</strong>bookmark you might already have installed.<br />

Note<br />

If you were to turn the registration into an Ajax-enabled area of your application, you could invoke a<br />

JavaScript function to call a <strong>PHP</strong> script to verify that a user’s email address <strong>and</strong> username are not already in<br />

the system. The function would also show an error if they are, <strong>and</strong> ultimately disallow the registration form<br />

to be submitted before these errors are corrected.<br />

Creating Additional Files<br />

As mentioned previously, you will add new files into the existing application structure.<br />

Although you will fill in these files as you go through the sections that follow, it is a<br />

good idea to get your bearings before you continue.<br />

Assume you will have at least two new files: a style sheet <strong>and</strong> a library of JavaScript<br />

functions. Create these two files now; call them new_ss.css <strong>and</strong> new_ajax.js.The new<br />

style sheet (new_ss.css) can be empty, because we haven’t yet defined new styles, but<br />

the new_ajax.js file should contain the getXMLHTTPRequest() function you created<br />

earlier in the chapter to create a new instance of the XMLHTTPRequest object in all<br />

browsers. Although you will be adding to these files, you can upload them as is to your<br />

web server at this time.<br />

The next step is to add a link to both these files in one of the existing display functions<br />

for the <strong>PHP</strong>bookmark application. Doing so will ensure that the styles from the<br />

style sheet are always available, as are the functions from the JavaScript library. If you<br />

recall from Chapter 27, the function that controls the output of the head element of the<br />

HTML (among other things) is called do_html_header(), <strong>and</strong> it resides in the<br />

output_fns.php file.<br />

A new version of this function is shown in Listing 34.4.<br />

Listing 34.4 Amended Version of do_html_header() Containing Links to the New<br />

Style Sheet <strong>and</strong> JavaScript Function Libraries<br />

function do_html_header($title) {<br />

// print an HTML header<br />

?><br />

<br />

<br />

<br />

<br />

body { font-family: Arial, Helvetica, sans-serif; font-size: 13px; }<br />

li, td { font-family: Arial, Helvetica, sans-serif; font-size: 13px; }<br />

hr { color: #3333cc; }<br />

a { color: #000000; }

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

Saved successfully!

Ooh no, something went wrong!