Goals
Learn about asynchronous communication
- the concept
- why and when it is superior to synchronous
- how JavaScript and XML (AJAX) implement it
- why and when XML is not useful
Key Concept
Rather than use hyperlinks (with URL parameters) and traditional form submission to pass information to a web server, information can be directly sent from web browser (client) to web server using an asynchronous http request initiated by JavaScript. In turn, JavaScript can then receive the response of the web server without the need to send an entire web page.
Pre-lab Activity
Read pages 373 to 388 in Learning PHP, MySQL, JavaScript, and CSS (2E) and answer the Pre-lab 8 quiz on Blackboard.
In-lab
Preparation
- Rather than copy all of the files from lab7, do all your work inside of
your lab7.
Connecting to web server via FTP
- Hostname: ftp.sienasellbacks.com
- Username: Your lastname @sienasellbacks.com
- Password: Told to you in lab3
- Settings: Use FTP; SFTP and SCP are not supported; If the option exists, use Passive FTP and
IPv6
Part 1: AJAX Basic Example
- Create a new file called ajax.js in lab7/js/ (yes, lab7) and add the following code:
function find() {
var name = document.getElementById("last").value;
var xmlhttp =new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
var content = xmlhttp.responseText;
document.getElementById("result").innerHTML = content;
}
}
xmlhttp.open("GET","quickfind.php?last="+name,true);
xmlhttp.send();
}
- Modify the existing file footer.html by adding a
script
tag
for ajax.js:
<script src="js/ajax.js"></script>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/doc.js"></script>
- Create a new file called quickfind.php in lab7
and add the following code:
quickfind.txt
- Create a new file called process.php in lab7
and add the following code:
process.txt
- Test process.php by uploading all the files you
just created and changed:
- js/ajax.js
- footer.html
- quickfind.php
- process.php
- Why exactly is this approach superior than the previous approach
shown in this example:
find.php
- Write your detailed answer in a text file called lab8.txt
Part 2: What you can do with AJAX
- Modify process.php in the following ways:
- Remove the submit button
- Add the following action to the "last" input text field:
onkeypress="find()"
- In lab8.txt, explain the profound change that we
made and list one advantage and one disadvantage to this approach.
Part 3: What is AJAX really good for?
Follow you instructor as he shows you how to make the delete really
really slick
Deliverables
- Upload lab8.txt to Blackboard.