What is AJAX? AJAX is a developer's dream, because you can:

Slides:



Advertisements
Similar presentations
AJAX Presented by: Dickson Fu Dimas Ariawan Niels Andreassen Ryan Dial Jordan Nielson CMPUT 410 University of Alberta 2006.
Advertisements

Multiple Tiers in Action
Web Development & Design Foundations with XHTML Chapter 9 Key Concepts.
Competence Development Introduction to AJAX. What is AJAX? AJAX = Asynchronous JavaScript and XML For creating interactive web applications – Exchanging.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Introduction to AJAX AJAX Keywords: JavaScript and XML
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
PHP and AJAX ISYS 475. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net,
Key question  What are the three standardized core languages used to implement web pages? Write the full name not the abbreviation and describe the “layer”
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
AJAX and Java ISYS 350. AJAX Asynchronous JavaScript and XML: – Related technologies: JavaScript, Document Object Model, XML, server-side script such.
06/10/2015AJAX 1. 2 Introduction All material from AJAX – what is it? Traditional web pages and operation Examples of AJAX use Creating.
Ajax. –Asynchronous JavaScript and XML –Umbrella term for technologies that often: Use client-side scripting for layout and formatting Use less than full.
Client side web programming Introduction Jaana Holvikivi, DSc. School of ICT.
1 Dr Alexiei Dingli XML Technologies XML Advanced.
Dynamic web content HTTP and HTML: Berners-Lee’s Basics.
AJAX محمد احمدی نیا 2 Of 27 What is AJAX?  AJAX = Asynchronous JavaScript and XML.  AJAX is not a new programming language, but.
Ajax XMod for Dummies Building DNN Ajax Modules Without Programming Dave McMullen SoCal DNN Users Group 2/7/07 –
AJAX stands for Asynchronous JavaScript And XML. AJAX is a type of programming made popular in 2005 by Google (with Google Suggest). AJAX is not a new.
School of Computing and Information Systems CS 371 Web Application Programming AJAX.
CISC 3140 (CIS 20.2) Design & Implementation of Software Application II Instructor : M. Meyer Address: Course Page:
SYST Web Technologies SYST Web Technologies AJAX.
INT222 - Internet Fundamentals Shi, Yue (Sunny) Office: T2095 SENECA COLLEGE.
AJAX Asynchronous JavaScript and XML 1. AJAX Outline What is AJAX? Benefits Real world examples How it works 2.
CHAPTER 13 COMMUNICATING WITH AJAX. LEARNING OBJECTIVES AJAX, which stands for Asynchronous JavaScript and XMLprovides a way for a browser to send and.
Dave Salinas. What is XML? XML stands for eXtensible Markup Language Markup language, like HTML HTML was designed to display data, whereas XML was designed.
AJAX AJAX Asynchronous JavaScript and XML --- MADHAVI
What is AJAX ? Asynchronous Javascript and XML. Not a stand-alone language or technology. It is a technique that combines a set of known technologies in.
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
JQuery, JSON, AJAX. AJAX: Async JavaScript & XML In traditional Web coding, to get information from a database or a file on the server –make an HTML form.
Ajax SUBMITTED BY NITIN RAMANI C.S.E 3 rd Y 5 th S R.N CS SUBMITTED TO PRO. PUSHPARAJ PATEL SIR.
Introduction to AJAX Pat Morin COMP Outline What is AJAX? – History – Uses – Pros and Cons An XML HTTP Transaction – Creating an XMLHTTPRequest.
What is AJAX ? Asynchronous Javascript and XML.
JavaScript and Ajax (Ajax Tutorial)
Introduction to Dynamic Web Programming
CSE 154 Lecture 11: AJAx.
Understanding XMLHttpRequest
Not a Language but a series of techniques
AJAX AJAX = Asynchronous JavaScript and XML.
CS 371 Web Application Programming
AJAX.
Callback Function function passed as a parameter to another function
AJAX – Asynchronous JavaScript and XML
Ajax Internet Engineering Fall 2017 Bahador Bakhshi
AJAX.
Web System & Technology
HTML Forms and User Input
AJAX (Asynchronous JavaScript and XML.)
CSE 154 Lecture 11: AJAx.
AJAX and JSP ISYS 350.
CSE 154 Lecture 22: AJAX.
NMD202 Web Scripting Week9.
HTML5 Level I Session I Chapter 1 - Introduction to Web Development
2018, Fall Pusan National University Ki-Joune Li
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
Web Programming Language
2017, Fall Pusan National University Ki-Joune Li
RESTful Web Services.
Exchange data interface
Lecture 12: The Fetch Api and AJAx
Lecture 12: The Fetch Api and AJAx
PHP Forms and Databases.
Web Technology Even Sem 2015
AJAX and JSP ISYS 350.
Web Client Side Technologies Raneem Qaddoura
PHP By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and.
Communicating with the Server
AJAX By Prof. B.A.Khivsara
Software Engineering for Internet Applications
Presentation transcript:

What is AJAX? AJAX is a developer's dream, because you can: Read data from a web server - after a web page has loaded Update a web page without reloading the page Send data to a web server - in the background What is AJAX? AJAX = Asynchronous JavaScript And XML. AJAX is not a programming language. AJAX just uses a combination of: A browser built-in XMLHttpRequest object (to request data from a web server) JavaScript and HTML DOM (to display or use the data)

The HTML page contains a <div> section and a <button>. The <div> section is used to display information from a server. The <button> calls a function (if it is clicked). The function requests data from a web server and displays it:

loadDoc()

Code Example <script> function loadDoc() { <!DOCTYPE html> <html> <body> <div id="demo"> <h2>The XMLHttpRequest Object</h2> <button type="button" onclick="loadDoc()">Change Content</button> </div> <script> function loadDoc() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("demo").innerHTML = this.responseText; } }; xhttp.open("GET", "ajax_info.txt", true); xhttp.send(); </script> </body> </html>

How Ajax Work?

The XMLHttpRequest Object All modern browsers support the XMLHttpRequest object. The XMLHttpRequest object can be used to exchange data with a web server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

Create an XMLHttpRequest Object All modern browsers (Chrome, Firefox, IE7+, Edge, Safari, Opera) have a built-in XMLHttpRequest object. Syntax for creating an XMLHttpRequest object:

GET or POST? GET is simpler and faster than POST, and can be used in most cases. However, always use POST requests when: A cached file is not an option (update a file or database on the server). Sending a large amount of data to the server (POST has no size limitations). Sending user input (which can contain unknown characters), POST is more robust and secure than GET. GET Requests

AJAX PHP <!DOCTYPE html> <html> <body> <h2>The XMLHttpRequest Object</h2> <h3>Start typing a name in the input field below:</h3> <p>Suggestions: <span id="txtHint"></span></p> <p>First name: <input type="text" id="txt1" onkeyup="showHint(this.value)"></p> <script> function showHint(str) { var xhttp; if (str.length == 0) { document.getElementById("txtHint").innerHTML = ""; return; } xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("txtHint").innerHTML = this.responseText; }; xhttp.open("GET", "gethint.php?q="+str, true); xhttp.send(); </script> </body> </html> AJAX PHP

gethint.php The PHP File - "gethint.php” <?php // Array with names $a[] = "Anna"; $a[] = "Brittany"; $a[] = "Cinderella"; $a[] = "Diana"; $a[] = "Eva"; $a[] = "Fiona"; $a[] = "Gunda"; $a[] = "Hege"; $a[] = "Inga"; $a[] = "Johanna"; $a[] = "Kitty"; $a[] = "Linda"; $a[] = "Nina"; $a[] = "Ophelia"; $a[] = "Petunia"; $a[] = "Amanda"; $a[] = "Raquel"; $a[] = "Cindy"; $a[] = "Doris"; $a[] = "Eve"; $a[] = "Evita"; $a[] = "Sunniva"; $a[] = "Tove"; $a[] = "Unni"; $a[] = "Violet"; $a[] = "Liza"; $a[] = "Elizabeth"; $a[] = "Ellen"; $a[] = "Wenche"; $a[] = "Vicky"; // get the q parameter from URL $q = $_REQUEST["q"]; $hint = ""; // lookup all hints from array if $q is different from ""  if ($q !== "") {   $q = strtolower($q);   $len=strlen($q);   foreach($a as $name) {     if (stristr($q, substr($name, 0, $len))) {       if ($hint === "") {         $hint = $name;       } else {         $hint .= ", $name";       }     }   } } // Output "no suggestion" if no hint was found or output correct values  echo $hint === "" ? "no suggestion" : $hint; ?> gethint.php The PHP File - "gethint.php” The PHP file checks an array of names, and returns the corresponding name(s) to the browser:

AJAX Database

The showCustomer() function does the following: Check if a customer is selected Create an XMLHttpRequest object Create the function to be executed when the server response is ready Send the request off to a file on the server Notice that a parameter (q) is added to the URL (with the content of the dropdown list)

The AJAX Server Page <?php $mysqli = new mysqli("servername", "username", "password", "dbname"); if($mysqli->connect_error) {   exit('Could not connect'); } $sql = "SELECT customerid, companyname, contactname, address, city, postalcode, country FROM customers WHERE customerid = ?"; $stmt = $mysqli->prepare($sql); $stmt->bind_param("s", $_GET['q']); $stmt->execute(); $stmt->store_result(); $stmt->bind_result($cid, $cname, $name, $adr, $city, $pcode, $country); $stmt->fetch(); $stmt->close(); echo "<table>"; echo "<tr>"; echo "<th>CustomerID</th>"; echo "<td>" . $cid . "</td>"; echo "<th>CompanyName</th>"; echo "<td>" . $cname . "</td>"; echo "<th>ContactName</th>"; echo "<td>" . $name . "</td>"; echo "<th>Address</th>"; echo "<td>" . $adr . "</td>"; echo "<th>City</th>"; echo "<td>" . $city . "</td>"; echo "<th>PostalCode</th>"; echo "<td>" . $pcode . "</td>"; echo "<th>Country</th>"; echo "<td>" . $country . "</td>"; echo "</tr>"; echo "</table>"; ?> The page on the server called by the JavaScript above is a PHP file called "getcustomer.php". The source code in "getcustomer.php" runs a query against a database, and returns the result in an HTML table:

<html> <head> <style> table, th, td {   border: 1px solid black;   border-collapse: collapse; } th, td {   padding: 5px; } </style> </head> <body> <table id="demo"></table> <script> function loadXMLDoc() {   var xmlhttp = new XMLHttpRequest();   xmlhttp.onreadystatechange = function() {     if (this.readyState == 4 && this.status == 200) {       myFunction(this);     }   };   xmlhttp.open("GET", "cd_catalog.xml", true);   xmlhttp.send(); } function myFunction(xml) {   var i;   var xmlDoc = xml.responseXML;   var table="<tr><th>Artist</th><th>Title</th></tr>";   var x = xmlDoc.getElementsByTagName("CD");   for (i = 0; i <x.length; i++) {      table += "<tr><td>" +     x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +     "</td><td>" +     x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +     "</td></tr>";   }   document.getElementById("demo").innerHTML = table; } </script> </body> </html> AJAX and XML

AJAX and XML Display the First CD in an HTML div Element <!DOCTYPE html> <html> <body> <div id='showCD'></div> <script> displayCD(0); function displayCD(i) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this, i); } }; xmlhttp.open("GET", "cd_catalog.xml", true); xmlhttp.send(); function myFunction(xml, i) { var xmlDoc = xml.responseXML; x = xmlDoc.getElementsByTagName("CD"); document.getElementById("showCD").innerHTML = "Artist: " + x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue + "<br>Title: " + x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + "<br>Year: " + x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue; </script> </body> </html> AJAX and XML

AJAX and XML Navigate Between the CDs Page 1 <!DOCTYPE html> <body> <div id='showCD'></div><br> <input type="button" onclick="previous()" value="<<"> <input type="button" onclick="next()" value=">>"> <script> var i = 0, len; displayCD(i); function displayCD(i) { var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this, i); } }; xmlhttp.open("GET", "cd_catalog.xml", true); xmlhttp.send(); AJAX and XML

AJAX and XML Navigate Between the CDs Page 2 function myFunction(xml, i) { var xmlDoc = xml.responseXML; x = xmlDoc.getElementsByTagName("CD"); len = x.length; document.getElementById("showCD").innerHTML = "Artist: " + x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue + "<br>Title: " + x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue + "<br>Year: " + x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue; } function next() { if (i < len-1) { i++; displayCD(i); function previous() { if (i > 0) { i--; </script> </body> </html> AJAX and XML