27.10.2015 Views

AJAX and PHP

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

save() <strong>and</strong> undo() are helper functions used for editing rows. The save function saves the<br />

original product values, which are loaded back to the grid by undo if the user changes her or his<br />

mind about the change <strong>and</strong> clicks the Cancel link.<br />

Row updating functionality is supported by the updateRow function, which is called when the Update<br />

link is clicked. updateRow() makes an asynchronous call to the server, specifying the new product<br />

values, which are composed into the query string using the createUpdateUrl helper function:<br />

// update one row in the grid if the connection is clear<br />

function updateRow(grid, productId)<br />

{<br />

// continue only if the XMLHttpRequest object isn't busy<br />

if (xmlHttp && (xmlHttp.readyState == 4 || xmlHttp.readyState == 0))<br />

{<br />

var query = feedGridUrl + "?action=UPDATE_ROW&id=" + productId +<br />

"&" + createUpdateUrl(grid);<br />

xmlHttp.open("GET", query, true);<br />

xmlHttp.onreadystatechange = h<strong>and</strong>leUpdatingRow;<br />

xmlHttp.send(null);<br />

}<br />

}<br />

The h<strong>and</strong>leUpdatingRow callback function has the responsibility to ensure that the product change<br />

is performed successfully, in which case it disables edit mode for the row, or displays an error<br />

message if an error happened on the server side:<br />

// continue only if HTTP status is "OK"<br />

if(xmlHttp.status == 200)<br />

{<br />

// read the response<br />

response = xmlHttp.responseText;<br />

// server error?<br />

if (response.indexOf("ERRNO") >= 0<br />

|| response.indexOf("error") >= 0<br />

|| response.length == 0)<br />

alert(response.length == 0 ? "Server serror." : response);<br />

// if everything went well, cancel edit mode<br />

else<br />

editId(editableId, false);<br />

}<br />

The technique for displaying the error was implemented in other exercises as well. If the server<br />

returned a specific error message, that message is displayed to the user. If <strong>PHP</strong> is configured not to<br />

output errors, the response from the server will be void, in which case we simply display a generic<br />

error message.<br />

Summary<br />

In this chapter you have implemented already familiar <strong>AJAX</strong> techniques to build a data grid. You<br />

have met XSL, which allows implementing very powerful architectures where the server side of<br />

your application doesn't need to deal with presentation.<br />

Having XSL deal with formatting the data to be displayed to your visitors is the professional way<br />

to deal with these kinds of tasks, <strong>and</strong> if you are serious about web development, it is recommended<br />

to learn XSL well. Beware; this will be time <strong>and</strong> energy consuming, but in the end the effort will<br />

be well worth it.<br />

221<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!