Competence Development Introduction to AJAX
What is AJAX? AJAX = Asynchronous JavaScript and XML For creating interactive web applications – Exchanging small amounts of data with the server ”behind the scenes” – Entire web pages do not have to be reloaded each time data is needed from the server Increases web pages interactivity, speed, functionality and usability Cross-platform technique usable on many different operating systems, computer architectures and web browsers Based on open standards such as JavaScript and DOM DOM (Document Object Model): a model in which the document or Web page contains objects (elements, links, etc.) that can be manipulated
What is AJAX? Asynchronous – extra data is requested from the server and loaded in the background without interfering the display and behavior of the existing page JavaScript is usually used for AJAX function calls Data is retrieved using the XMLHttpRequest object that is available to scripting languages run in modern browsers Alternatively, Remote Scripting can be used in browsers that do not support XMLHttpRequest. Asynchronous content does not need to be formatted in XML
Prototype – JavaScript Framework Unique, easy-to-use toolkit for class-driven development with one of the nicest Ajax libraries around Easy Ajax and DOM manipulation for dynamic web applications Quickly becoming the codebase of choice for web application developers everywhere
Example #1: Instructions Display contents of a text file in HTML DIV –element using AJAX when a button is pressed Basically this means that we will create a web page that calls a script in another file and then displays the result on our web page Of course we also need to create a DIV and a button No error checking is done, this is just a basic example
Example #1: First steps Download prototype.js and place it in your project directory Create text file to the same directory, e.g. testi.txt and add some data to it Create file (index.php) to the same directory, we use it as our web page Create file (skripti.php) to the same directory, we use it as our script file for reading data from another file
Example #1: Our web page Ajax example #1 JavaScript code is placed between HEAD-tags Create DIV and BUTTON between BODY-tags
Example #1: Our web page index.php Ajax example #1 function lue() { new Ajax.Request('skripti.php', { method: 'get', onSuccess: function(transport) { var tulos = $('tulos'); tulos.update(transport.responseText); } }); }
Example #1: Our script skripti.php <?php $tiedosto = file("testi.txt"); foreach($tiedosto as $rivi) print $rivi." "; ?> testi.txt example: KALLE KARI VILLE PEKKA TIMO JARI
Example #1: Results Test this example: Files: index.php skripti.php testi.txt prototype.js
Example #2: Instructions Save HTML form fields to text file and print loading and result message to HTML DIVs We create skripti.php like in previous example that saves submitted form data into a text file Our script will return an error message if file opening fails
Example #2: First steps Create index.php and skripti.php like in first example Make sure that directory has write rights (this can be changed with FTP client or by executing chmod +w manually for the directory)
Example #2: Our web page index.php Ajax example #2 function init() { $('submit').onclick = function () { sendData(); } function sendData() { var url = 'skripti.php'; var pars = Form.serialize('frm'); var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: showLoad, onComplete: showResponse} ); } function showLoad() { $('load').style.display = 'block'; }
Example #2: Our web page cont. function showResponse(originalRequest) { var newData = originalRequest.responseText; $('load').style.display = 'none'; $('status').update(newData); } Etunimi: Sukunimi: Tallennetaan...
Example #2: Our script skripti.php <?php // luodaan tiedosto $tiedosto = fopen("tiedot.txt", "w"); if(!$tiedosto) print "Tiedoston aukaiseminen ei onnistunut!"; // kirjoitetaan vastaanotetut tiedot tiedostoon foreach($_GET as $tieto) fwrite($tiedosto, $tieto."\n"); print "Tiedot tallennettu onnistuneesti!"; ?>
Example #2: Results Test this example: Files: index.php skripti.php prototype.js
References
To be continued...