Running ajax. Entering the zip and clicking elsewhere 81411.

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
AJAX Presented by: Dickson Fu Dimas Ariawan Niels Andreassen Ryan Dial Jordan Nielson CMPUT 410 University of Alberta 2006.
AJAX asynchronous server-client communication. Test.
AJAX/JavaScript csc667/867 Spring 2006 Ilmi Yoon Slide Courtesy to ClearNova & degrave.com.
XMLHttpRequest Object and XML What we should learn in this lesson –What is the XHR object? –How to create the XHR objects? –XHR object properties –XHR.
PHP with XML & AJAX XML: Extensible Markup Language AJAX: Asynchronous JavaScript and XML.
Chapter 6 DOJO TOOLKITS. Objectives Discuss XML DOM Discuss JSON Discuss Ajax Response in XML, HTML, JSON, and Other Data Type.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Internet Applications Spring Review Last week –MySQL –Questions?
Ajax (Asynchronous JavaScript and XML). AJAX  Enable asynchronous communication between a web client and a server.  A client is not blocked when an.
CGI and AJAX CS-260 Dick Steflik.
PHP and AJAX ISYS 475. AJAX Asynchronous JavaScript and XML: – JavaScript, Document Object Model, Cascade Style Sheet, XML, server-side script such as.Net,
JavaScript & jQuery the missing manual Chapter 11
Lecture 12 – AJAX SFDV3011 – Advanced Web Development Reference: 1.
Ajax - Part II Communicating with the Server. Learning Objectives By the end of this lecture, you should be able to: – Describe the overview of steps.
JavaScript, Fourth Edition Chapter 12 Updating Web Pages with AJAX.
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.
Client side web programming Introduction Jaana Holvikivi, DSc. School of ICT.
Ajax - 1h. AJAX IS: A browser technology A technology that uses only JavaScript in the browser page A very efficient way of updating information A way.
1 Dr Alexiei Dingli XML Technologies XML Advanced.
15/10/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Introduction to MySQL Lab no. 10 Advance Database Management System.
PHP MySQL Introduction. MySQL is the most popular open-source database system. What is MySQL? MySQL is a database. The data in MySQL is stored in database.
Website Development with PHP and MySQL Saving Data.
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 –
Asynchronous Javascript And XML AJAX : an introduction UFCEUS-20-2 : Web Programming.
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.
Web Technology Introduction AJAXAJAX. AJAX Outline  What is AJAX?  Benefits  Real world examples  How it works  Code review  Samples.
ASP. What is ASP? ASP stands for Active Server Pages ASP is a Microsoft Technology ASP is a program that runs inside IIS IIS stands for Internet Information.
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:
AJAX Asynchronous JavaScript & XML A short introduction.
Creating Dynamic Webpages
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.
CHAPTER 8 AJAX & JSON WHAT IS AJAX? Ajax lets you…
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
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.
AJAX – Asynchronous JavaScript And XML By Kranthi Kiran Nuthi CIS 764 Kansas State University.
INNOV-2: Build a Better Web Interface Using AJAX Chris Morgan Pandora Software Systems
1 AJAX. AJAX – Whatzit? Asynchronous (content loading)‏ Javascript (logic & control)‏ And XML (request handling)‏
JavaScript and Ajax Week 10 Web site:
NCCUCS 軟體工程概論 Lecture 5: Ajax, Mashups April 29, 2014.
AJAX. Objectives Understand and apply AJAX Using AJAX in DOJO library.
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)
Not a Language but a series of techniques
AJAX AJAX = Asynchronous JavaScript and XML.
PHP Database ODBC Create an ODBC Connection
CS 371 Web Application Programming
AJAX and REST.
XMLHttp Object.
Ajax Internet Engineering Fall 2017 Bahador Bakhshi
Web System & Technology
AJAX (Asynchronous JavaScript and XML.)
AJAX and JSP ISYS 350.
ISC440: Web Programming 2 AJAX
JavaScript & jQuery AJAX.
AJAX CS-422 Dick Steflik.
Web Technology Even Sem 2015
AJAX and JSP ISYS 350.
Software Engineering for Internet Applications
Presentation transcript:

Running ajax

Entering the zip and clicking elsewhere 81411

2 nd example (next 3 slides) uses perl cgi Simple Ajax Example Ajax is the method of using Javascript, DHTML and the XMLHttpRequest object to perform a GET or POST and return a result without reloading the HTML page. Below is a very simple Ajax example that calls a CGI script that prints out the word sent to the CGI script and the remote user's IP address. Simple Ajax Demo word: The secret word is excellent and your IP is

The html/js Simple Ajax Example function xmlhttpPost(strURL) { var xmlHttpReq = false; var self = this; // Mozilla/Safari if (window.XMLHttpRequest) { self.xmlHttpReq = new XMLHttpRequest(); } // IE else if (window.ActiveXObject) { self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP"); } self.xmlHttpReq.open('POST', strURL, true); self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); self.xmlHttpReq.onreadystatechange = function() { if (self.xmlHttpReq.readyState == 4) { updatepage(self.xmlHttpReq.responseText); } } self.xmlHttpReq.send(getquerystring()); } function getquerystring() { var form = document.forms['f1']; var word = form.word.value; qstr = 'w=' + escape(word); // NOTE: no '?' before querystring return qstr; } function updatepage(str){ document.getElementById("result").innerHTML = str; } word:

A cgi perl script (put into cgi-bin dir) #!/usr/bin/perl -w use CGI; $query = new CGI; $secretword = $query->param('w'); $remotehost = $query->remote_host(); print $query->header; print " The secret word is $secretword and your IP is $remotehost

My script #!c:\perl\bin\perl.exe use CGI; $query = new CGI; $secretword = $query->param('w'); $remotehost = $query->remote_host(); print $query->header; print " The secret word is $secretword and your IP is $remotehost "

On my machine

A similar example done slightly differently function loadXMLDoc(url) { if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET",url,false); xmlhttp.send(null); document.getElementById('test').innerHTML=xmlhttp.responseText; } Click to let AJAX change this text Click Me Click Me

About the server side Important Methods The XMLHttpRequest object has 2 important methods: The open() method The send() method Sending an AJAX Request to a Server To send a request to a web server, we use the open() and send() methods. The open() method takes three arguments. The first argument defines which method to use (GET or POST). The second argument specifies the name of the server resource (URL). The third argument specifies if the request should be handled asynchronously. The send() method sends the request off to the server. If we assume that requested a file called "time.asp", the code would be: url="time.asp" xmlhttp.open("GET",url,true); xmlhttp.send(null);In the example we assume that the current web page and the requested resource are both in the same file directory. Important Properties The XMLHttpRequest object has 3 important properties: The responseText property The readyState property The onreadystatechange property The responseText property The XMLHttpRequest object stores any data retrieved from a server as a result of a server request in its responseText property. In the previous chapter we copied the content of the responseText property into our HTML with the following statement: document.getElementById('test').innerHTML=xmlhttp.responseText

Server side continued XMLHttpRequest Open - Using False In the examples (from the previous pages) we used this simplified syntax: xmlhttp.open("GET",url,false); xmlhttp.send(null); document.getElementById('test').innerHTML=xmlhttp.responseText;The third parameter in the open call is "false". This tells the XMLHttpRequest object to wait until the server request is completed before next statement is executed. For small applications and simple server request, this might be OK. But if the request takes a long time or cannot be served, this might cause your web application to hang or stop. XMLHttpRequest Open - Using True By changing the third parameter in the open call to "true", you tell the XMLHttpRequest object to continue the execution after the request to the server has been sent. Because you cannot simply start using the response from the server request before you are sure the request has been completed, you need to set the onreadystatechange property of the XMLHttpRequest, to a function (or name of a function) to be executed after completion. In this onreadystatechange function you must test the readyState property before you can use the result of the server call. Simply change the code to: xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) {document.getElementById('test').innerHTML=xmlhttp.responseText} } xmlhttp.open("GET",url,true); xmlhttp.send(null); The readyState property The readyState property holds the status of the server's response. Possible values for the readyState property: StateDescription0The request is not initialized1The request has been set up2The request has been sent3The request is in process4The request is complete

The onreadystatechange property: Update innerHTML on a div of the DOM when server returns The onreadystatechange property stores a function (or the name of a function) to be called automatically each time the readyState property changes. You can store a full function in the property like this: xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) {document.getElementById('test').innerHTML=xmlhttp.responseText} } xmlhttp.open("GET",url,true); xmlhttp.send(null);Or you can store the name of a function like this: xmlhttp.onreadystatechange=state_Change xmlhttp.open("GET",url,true); xmlhttp.send(null); function state_Change() { if(xmlhttp.readyState==4) {document.getElementById('test').innerHTML=xmlhttp.responseText} }

This example gets a text file Put two text files in www. There is no php function to supply.

About ajax XMLHttpRequest Object can Request any Data With the XMLHttpRequest object, you can request any web resource from a server. You can request TXT files, HTML files, XML files, pictures or any data that is accessible from the Internet. AJAX is about creating clever applications that can use the data. Requesting Text Files: Many AJAX applications request pure text files to retrieve data for the application. Requesting XML Files: A very common AJAX method is to request XML files to retrieve application data. Requesting ASP or PHP (or Perl) Files: Requesting an ASP or PHP file is the most common way to access database information. Requesting HTML Files: Requesting HTML files is a common method for filling out different information on a web page Submitting Forms: With AJAX you can easily submit form data without having to reload the page. In the following examples we will do a little bit of each.

Guess a name (html part) First Name: Suggestions:

clienthint.js var xmlhttp function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support XMLHTTP!"); return; } var url="gethint.asp"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }

gethint.php builds an array& sends a hint <?php // Fill up 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=$_GET["q"]; //lookup all hints from array if length of q>0 if (strlen($q) > 0) { $hint=""; for($i=0; $i

Running this example – looks up match for strlen of entered text

Further examples…next 4 slides for lookup user example (uses mysql) Select a User: Peter Griffin Lois Griffin Glenn Quagmire Joseph Swanson Person info will be listed here.

Php script Firstname Lastname Age Hometown Job "; while($row = mysql_fetch_array($result)) { echo " "; echo " ". $row['FirstName']. " "; echo " ". $row['LastName']. " "; echo " ". $row['Age']. " "; echo " ". $row['Hometown']. " "; echo " ". $row['Job']. " "; echo " "; } echo " "; mysql_close($con); ?>

Selectusers.js var xmlhttp; function showUser(str) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="getuser.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }

Running php

notes You need to build the mysql db and table and have mysql running

Using DOMDocument and simplexml file reader The following ajax examples require php5. You can upgrade instantrails to php5. This may prove tricky. I used an xampp installation I had. Xampp comes with php5.

A php which displays some xml content (no ajax) <?php // load file if (file_exists('pet.xml')) { $xml = simplexml_load_file('pet.xml'); print_r($xml); } else { exit('Failed to open test.xml.'); } //$xml = simplexml_load_file($file) or die ("Unable to load XML file!"); // access XML data echo " Name: ". $xml->name. "\n"; echo " Age: ". $xml->age. "\n"; echo " Species: ". $xml->species. "\n"; echo " Parents: ". $xml->parents->mother. " and ". $xml->parents->father. "\n"; ?>

The xml Polly Parrot 3 parrot Pia Parrot Peter Parrot

Running php

Interacting with xml Select a CD: Bob Dylan Bonnie Tyler Dolly Parton CD info will be listed here.

Js file var xmlhttp function showCD(str) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support AJAX!"); return; } var url="getcd.asp"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }

Getcd.php load("cd_catalog.xml"); $x=$xmlDoc->getElementsByTagName('ARTIST'); for ($i=0; $i length-1; $i++) { //Process only element nodes if ($x->item($i)->nodeType==1) { if ($x->item($i)->childNodes->item(0)->nodeValue == $q) { $y=($x->item($i)->parentNode); } } } $cd=($y->childNodes); for ($i=0;$i length;$i++) { //Process only element nodes if ($cd->item($i)->nodeType==1) { echo(" ". $cd->item($i)->nodeName. ": "); echo($cd->item($i)->childNodes->item(0)->nodeValue); echo(" "); } } ?>

Xml file <!-- Edited by XMLSpy® --> Empire Burlesque Bob Dylan USA Columbia Hide your heart Bonnie Tyler UK CBS Records

Need php5 to run the xml stuff

A live search (using xml)

The html #livesearch { margin:0px; width:194px; } #txt1 { margin:0px; }

Livesearch.php <?php $xmlDoc = new DOMDocument(); $xmlDoc->load("links.xml"); $x=$xmlDoc->getElementsByTagName('link'); //get the q parameter from URL $q=$_GET["q"]; //lookup all links from the xml file if length of q>0 if (strlen($q) > 0) { $hint=""; for($i=0; $i length); $i++) { $y=$x->item($i)->getElementsByTagName('title'); $z=$x->item($i)->getElementsByTagName('url'); if ($y->item(0)->nodeType==1) { //find a link matching the search text if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) { if ($hint=="") { $hint="<a href='". $z->item(0)->childNodes->item(0)->nodeValue. "' target='_blank'>". $y->item(0)->childNodes->item(0)->nodeValue. " "; } else { $hint=$hint. " <a href='". $z->item(0)->childNodes->item(0)->nodeValue. "' target='_blank'>". $y->item(0)->childNodes->item(0)->nodeValue. " "; } } } }} // Set output to "no suggestion" if no hint were found // or to the correct values if ($hint == "") { $response="no suggestion"; } else { $response=$hint; } //output the response echo $response; ?>

An xml file of links HTML DOM alt Property HTML DOM height Property HTML a tag HTML br tag CSS background Property CSS border Property JavaScript Date() Method JavaScript anchor() Method

livesearch.js var xmlhttp; function showResult(str) { if (str.length==0) { document.getElementById("livesearch").innerHTML=""; document.getElementById("livesearch").style.border="0px"; return; } xmlhttp=GetXmlHttpObject() if (xmlhttp==null) { alert ("Your browser does not support XML HTTP Request"); return; } var url="livesearch.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged ; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("livesearch").innerHTML=xmlhttp.r esponseText; document.getElementById("livesearch").style.border="1px solid #A5ACB2"; } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }

Rss feed hanlder: requires php5

An html populated with a few feeds Select an RSS-feed: Google News MSNBC News RSS-feed will be listed here...

The javascript var xmlhttp; function showRSS(str) { xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support XML HTTP Request"); return; } var url="getrss.php"; url=url+"?q="+str; url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null);} function stateChanged() {if (xmlhttp.readyState==4) {document.getElementById("rssOutput").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } return null;}

getrss.php <?php //get the q parameter from URL $q=$_GET["q"]; //find out which feed was selected if($q=="Google") {$xml=(" ; } elseif($q=="MSNBC") { $xml=(" } $xmlDoc = new DOMDocument(); $xmlDoc->load($xml); //get elements from " " $channel=$xmlDoc->getElementsByTagName('channel')->item(0); $channel_title = $channel->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $channel_link = $channel->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $channel_desc = $channel->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; //output elements from " " echo(" <a href='". $channel_link. "'>". $channel_title. " "); echo(" "); echo($channel_desc. " "); //get and output " " elements $x=$xmlDoc->getElementsByTagName('item'); for ($i=0; $i<=2; $i++) { $item_title=$x->item($i)->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; echo (" <a href='". $item_link. "'>". $item_title. " "); echo (" "); echo ($item_desc. " "); } ?>