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.

<strong>AJAX</strong> <strong>and</strong> the Future of Web Applications<br />

If the user name received from the client is empty, the message will be, "Stranger, please tell me your<br />

name!". If the name is Cristian, Bogdan, Filip, Mihai, or Yoda, the server responds with "Hello, master<br />

!". If the name is anything else, the message will be ", I don't know you!".<br />

So if Mickey Mouse types his name, the server will send back the following XML structure:<br />

24<br />

<br />

<br />

Mickey Mouse, I don't know you!<br />

<br />

The quickstart.php script starts by generating the XML document header <strong>and</strong> the opening<br />

element:<br />

), making sure<br />

the message will be safely displayed in the web browser eliminating potential problems <strong>and</strong><br />

security risks.<br />

Formatting the text on the server for the client (instead of doing this directly at the client) is<br />

actually a bad practice when writing production code. Ideally, the server's responsibility is<br />

to send data in a generic format, <strong>and</strong> it is the recipient's responsibility to deal with security<br />

<strong>and</strong> formatting issues. This makes even more sense if you think that one day you may need<br />

to insert exactly the same text into a database, but the database will need different<br />

formatting sequences (in that case as well, a database h<strong>and</strong>ling script would do the<br />

formatting job, <strong>and</strong> not the server). For the quickstart scenario, formatting the HTML in<br />

<strong>PHP</strong> allowed us to keep the code shorter <strong>and</strong> simpler to underst<strong>and</strong> <strong>and</strong> explain.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!