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.

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

Configuring the Form Submission Event H<strong>and</strong>ler for Deletion<br />

Confirming event deletion requires a little more modification to init.js. To execute properly, the value<br />

of the Submit button needs to be stored <strong>and</strong> passed to the processing file. This is because the form can<br />

be submitted with either Yes, Delete It or Nope! Just Kidding! as values; the script checks which<br />

button was clicked to determine what action to take.<br />

To store the button’s value, use the this keyword as the <strong>jQuery</strong> selector, <strong>and</strong> then store the returned<br />

string from .val() as a variable called submitVal. Next, check whether the button’s name attribute is<br />

confirm_delete. If so, append the action confirm_delete <strong>and</strong> the value of the button to the query string<br />

before submitting it.<br />

Insert the following code shown in bold to accomplish this:<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 />

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

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

// Stores the value of the submit button<br />

submitVal = $(this).val();<br />

// If this is the deletion form, appends an action<br />

if ( $(this).attr("name")=="confirm_delete" )<br />

{<br />

// Adds necessary info to the query string<br />

formData += "&action=confirm_delete"<br />

+ "&confirm_delete="+submitVal;<br />

}<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 />

// If this is a new event, adds it to<br />

// the calendar<br />

if ( $("[name=event_id]").val().length==0 )<br />

{<br />

fx.addevent(data, formData);<br />

}<br />

},<br />

error: function(msg) {<br />

alert(msg);<br />

}<br />

301

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

Saved successfully!

Ooh no, something went wrong!