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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

event_title=Test+Event&event_start=2010-01-04+08%3A00%3A00&event_end=2010-01-04+10%3A00 <br />

%3A00&event_description=This+is+a+test+description.&event_id=&token=a52412c2e7bfb993844 <br />

0dc9d4e0867370e350134&action=event_edit<br />

Submitting the Serialized Form Data to the <strong>Pro</strong>cessing File<br />

Now that the form data is serialized, you’re ready use $.ajax() to send the data to the processing file.<br />

Use the POST method to submit the serialized data to ajax.inc.php, <strong>and</strong> then fade out the modal<br />

window <strong>and</strong> overlay using fx.boxout() on a successful submission. Also, log a confirmation message in<br />

the Firebug console <strong>and</strong> append the following bold code to init.js:<br />

// Edits events without reloading<br />

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

});<br />

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

event.preventDefault();<br />

// Serializes the form data for use with $.ajax()<br />

var formData = $(this).parents("form").serialize();<br />

// Sends the data to the processing file<br />

$.ajax({<br />

type: "POST",<br />

url: processFile,<br />

data: formData,<br />

success: function(data) {<br />

// Fades out the modal window<br />

fx.boxout();<br />

// Logs a message to the console<br />

console.log( "Event saved!" );<br />

},<br />

error: function(msg) {<br />

alert(msg);<br />

}<br />

});<br />

At this point, the script is ready to save new events. First, however, you need to modify ajax.inc.php<br />

to accept this data.<br />

Modifying the AJAX <strong>Pro</strong>cessing File to H<strong>and</strong>le New Submissions<br />

Getting ajax.inc.php ready to accept submissions from the event editing form is as easy as adding a new<br />

element to the lookup array:<br />

271

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

Saved successfully!

Ooh no, something went wrong!