Cse2045Y Web Application Development: Ajax (Asynchronous Javascript and XML)
Cse2045Y Web Application Development: Ajax (Asynchronous Javascript and XML)
Cse2045Y Web Application Development: Ajax (Asynchronous Javascript and XML)
Lecture 2
AJAX (Asynchronous JavaScript And XML)
Agenda
• What is AJAX?
• The AJAX model
• Why use AJAX
• How AJAX works?
• AJAX example using:
– GET
– POST
2
What is AJAX?
• AJAX = Asynchronous JavaScript and XML.
http://blog.arvixe.com/wp-content/uploads/2013/08/ajax-diagram.png
4
Why use AJAX?
• AJAX is a technique for creating fast and dynamic web
pages.
https://www.w3schools.com/php/ajax.gif
How AJAX Works (2)
• AJAX is based on internet standards, and uses a
combination of:
– XMLHttpRequest object (to exchange data asynchronously
with a server)
– JavaScript/DOM (to display/interact with the information)
– CSS (to style the data)
– XML (often used as the format for transferring data)
function ajaxGET()
get.html
formGET.php
get.html
<html>
<head>
<script type="text/javascript" src="ajax_functions.js"></script>
</head>
<body>
</body></html>
get.html
• A Javascript function ajaxGET() is called when the
submit button is clicked.
<input type="button" value="submit"
onClick="ajaxGET();" />
function ajaxGET()
function ajaxGET(){
var http_request= new XMLHttpRequest();
var namevalue=document.getElementById("name").value;
var agevalue=document.getElementById("age").value;
http_request.open("GET", "formGET.php?name="+namevalue+"&age="+agevalue, true);
http_request.onreadystatechange=function(){
if (http_request.readyState==4){
if (http_request.status==200) {
document.getElementById("result").innerHTML=http_request.responseText;
}
else{
alert("An error has occured making the request");
}
}
}
http_request.send(null);
}//end function ajaxGET()
function ajaxGET()
• ajaxGET() builds an http request to
formGET.php with appropriate
parameters (name & age).
var namevalue=document.getElementById("name").value;
var agevalue=document.getElementById("age").value;
http_request.open("GET", "formGET.php?
name="+namevalue+ "&age="+agevalue, true);
function ajaxGET()
• The innerHTML property of the <div> is
used to display the response.
http_request.onreadystatechange=function(){
if (http_request.readyState==4){
if (http_request.status==200) {
document.getElementById("result").innerHTML=
http_request.responseText;
}
else{
alert("An error has occured making the request");
}
}
}
formGET.php
<?php
$name=$_GET['name'];
$age=$_GET['age'];
formGET.php
• Name & Age are retrieved using the GET
method.
$name=$_GET['name'];
$age=$_GET['age'];
</body></html>
function ajaxPOST()
function ajaxPOST(){
var http_request= new XMLHttpRequest();
var namevalue=document.getElementById("name").value;
var agevalue=document.getElementById("age").value;
var parameters="name="+namevalue+"&age="+agevalue;
http_request.open("POST", "formPOST.php", true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.onreadystatechange=function(){
if (http_request.readyState==4){
if (http_request.status==200) {
document.getElementById("result").innerHTML=http_request.responseText;
}
else{
alert("An error has occured making the request");
}
}
}
http_request.send(parameters);
}//end function ajaxpost
formPOST.html
<?php
$name=$_POST['name'];
$age=$_POST['age'];
Activity 1
• Consider the following form addModule.php,
used for admin section to enter modules into the
system. addModule.php is submitted to
addmodule_process.php using the GET
method.
Activity 1
• Requirements:
– Each time the user types the module code, a request is
sent to check if this module already exists.
Activity 1
• Modify the codes in addModule.php (given in next
slide) such that if the module code exists, a
message is displayed next to the module code field
to inform the user that this module already exists.
Database
• Assume that the following schema has
been defined for the database,
implemented in MySQL.
db_connect.php
<?php
$strHost = "localhost";
$strUser = "root";
$strPass = "";
$strDb = "feedbackdatabase";
// open connection
$con = mysqli_connect($strHost, $strUser, $strPass, $strDb);
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
checkModule.php
<?php
// MISSING CODES 1 - Get module code
require("db_connect.php");
mysqli_close($con);
?>
31