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.

}<br />

"boxin" : function(data, modal) {...},<br />

"boxout" : function(event) {...}<br />

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

$(".admin").live("click", function(event){...});<br />

// Make the cancel button on editing forms behave like the<br />

// close button <strong>and</strong> fade out modal windows <strong>and</strong> overlays<br />

$(".edit-form a:contains(cancel)").live("click", function(event){<br />

fx.boxout(event);<br />

});<br />

});<br />

CHAPTER 8 ■ EDITING THE CALENDAR WITH AJAX AND JQUERY<br />

Save the file, reload http://localhost/, <strong>and</strong> click the Add a New Event button. After the modal<br />

window has loaded, click the Cancel link in the form. The modal window <strong>and</strong> overlay will fade out, just<br />

as they do when the Close button is clicked.<br />

Saving New Events in the Database<br />

To make the form work properly, you must now add a click event h<strong>and</strong>ler to the Submit button on the<br />

form. This h<strong>and</strong>ler will prevent the default form submission, then use .serialize() to create a query<br />

string from the form inputs. It then uses the serialized data to submit the form via POST to ajax.inc.php.<br />

Start <strong>by</strong> adding a new click h<strong>and</strong>ler to any inputs of type submit that exist in a form with the editform<br />

class. Using .live() ensures that dynamically created inputs will still be targeted <strong>by</strong> the h<strong>and</strong>ler.<br />

You can prevent the default action using event.preventDefault().<br />

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

<strong>jQuery</strong>(function($){<br />

var processFile = "assets/inc/ajax.inc.php",<br />

fx = {<br />

"initModal" : function() {...},<br />

"boxin" : function(data, modal) {...},<br />

"boxout" : function(event) {...}<br />

}<br />

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

$(".admin").live("click", function(event){...});<br />

// Edits events without reloading<br />

$(".edit-form input[type=submit]").live("click", function(event){<br />

// Prevents the default form action from executing<br />

event.preventDefault();<br />

// Logs a message to indicate the script is working<br />

269

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

Saved successfully!

Ooh no, something went wrong!