02.06.2013 Views

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

Pro PHP and jQuery by Jason Lengstorf.pdf - Computer Science ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

248<br />

CHAPTER 7 ■ ENHANCING THE USER INTERFACE WITH JQUERY<br />

Creating a File to H<strong>and</strong>le AJAX Requests<br />

Before you put together the call to $.ajax(), it helps to know where <strong>and</strong> how the data should be sent. In<br />

the inc folder, create a new file called ajax.inc.php (/public/assets/inc/ajax.inc.php). This file will<br />

work very similarly to process.inc.php, except it will deal exclusively with AJAX calls. Because a value<br />

returned from a <strong>PHP</strong> function can’t be read <strong>by</strong> JavaScript unless the value is actually output (using echo<br />

or its ilk), process.inc.php will not function properly for this aspect of the application.<br />

Essentially, ajax.inc.php will use a lookup array to determine which objects <strong>and</strong> methods need to<br />

be used, then output the returned values using echo for use with AJAX.<br />

Start <strong>by</strong> enabling sessions, loading the necessary configuration information, defining a constant,<br />

<strong>and</strong> putting together an auto-load function. Now add the following to ajax.inc.php:<br />

<br />

Next, define the lookup array with information for loading event data, then put together the code<br />

that will instantiate an object, call the method, <strong>and</strong> output the returned value using the bold code that<br />

follows:<br />

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

Saved successfully!

Ooh no, something went wrong!