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.

Pulls up events in a modal window<br />

$("li>a").live("click", function(event){<br />

});<br />

});<br />

// Stops the link from loading view.php<br />

event.preventDefault();<br />

// Adds an "active" class to the link<br />

$(this).addClass("active");<br />

// Gets the query string from the link href<br />

var data = $(this)<br />

.attr("href")<br />

.replace(/.+?\?(.*)$/, "$1"),<br />

// Checks if the modal window exists <strong>and</strong><br />

// selects it, or creates a new one<br />

modal = fx.initModal();<br />

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

Next, set up the call to $.ajax() in the event h<strong>and</strong>ler. It will use the POST method, point to the<br />

processFile, <strong>and</strong> send the appropriate data. Because the query string extracted from the link does not<br />

include an action field, insert one manually here. Finally, use .append() to insert the returned markup<br />

into the modal window if the call succeeds or to display an error message if it fails.<br />

Do this <strong>by</strong> inserting the following bold lines into init.js:<br />

// Pulls up events in a modal window<br />

$("li>a").live("click", function(event){<br />

// Stops the link from loading view.php<br />

event.preventDefault();<br />

// Adds an "active" class to the link<br />

$(this).addClass("active");<br />

// Gets the query string from the link href<br />

var data = $(this)<br />

.attr("href")<br />

.replace(/.+?\?(.*)$/, "$1"),<br />

// Checks if the modal window exists <strong>and</strong><br />

// selects it, or creates a new one<br />

modal = fx.initModal();<br />

// Loads the event data from the DB<br />

$.ajax({<br />

type: "POST",<br />

url: processFile,<br />

251

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

Saved successfully!

Ooh no, something went wrong!