Ajax
Ajax
Ullrich Hustadt
1 Ajax
Motivation
Overview
Creating a HTTP Request
Creating a HTTP Response
Processing a HTTP Response
2 Further Reading
Ajax: Motivation
< form id = ' pc ' >< label > Postcode : </ label >
< input type = ' text ' name = ' pc ' value = ' ' >
<br > < label > Address : </ label >
< select name = ' adr ' id = ' adr ' > </ select >
</ form >
• Entering an address into an HTML form is often a two-stage process
1 Enter a postcode into a text field
2 Select an entry from a drop-down menu of all addresses for the entered
postcode
• To implement this process using PHP
• Step 1 needs to result in a form submission to a PHP script
• The PHP script needs to retrieve the addresses for the given postcode from
a dabase
• The PHP script then produces a complete HTML document with an
updated form containing options for the drop-down menu
• That HTML document is send back
• The user can then perform Step 2
COMP519 Web Programming Lecture 26 Slide L26 – 2
Ajax Motivation
Ajax: Motivation
< form id = ' pc ' >< label > Postcode : </ label >
< input type = ' text ' name = ' pc ' value = ' ' >
<br > < label > Address : </ label >
< select name = ' adr ' id = ' adr ' > </ select >
</ form >
• Entering an address into an HTML form is often a two-stage process
1 Enter a postcode into a text field
2 Select an entry from a drop-down menu of all addresses for the entered
postcode
• To implement this process using JavaScript
• A JavaScript event handler is triggered when the user has completed Step 1
• The JavaScript event handler uses an HTTP request to get the data for the
drop-down menu
• In response the the HTTP request, a PHP script retrieves the addresses for
the given postcode from a database and sends them back
• The JavaScript event handler constructs the drop-down menu
• The user can then perform Step 2
COMP519 Web Programming Lecture 26 Slide L26 – 3
Ajax Overview
Ajax: Overview
• Ajax, Asynchronous JavaScript and XML, is a set of JavaScript
methods related to XMLHttpRequest objects and patterns for their use
• Ajax allows web applications to send and retrieve data from a server
asynchronously (in the background)
• On the server-side, PHP scripts are typically used to receive / send data
and to deal with related database transactions
• Historically, data was transferred in XML format though nowadays use
of JSON is much more common
Ajax: Overview
K. Ramirez: Build Ajax-based Web sites with PHP. IBM, 2 Sep 2008.
https://www.ibm.com/developerworks/library/wa-aj-php/
[accessed 21 Nov 2019]
COMP519 Web Programming Lecture 26 Slide L26 – 5
Ajax Creating a HTTP Request
JSON.parse(text [, reviver])
Parses a JSON string text and returns the corresponding JavaScript
value transformed further by the optional reviver function
JSON.stringify(value [, replacer [, space]])
Converts a JavaScript value to a JSON string value, optionally
replacing values if a function replacer and adding space as
specified by space
• Read
• Chapter 18: Using Asynchronous Communication
of R. Nixon: Learning PHP, MySQL & JavaScript:
with jQuery, CSS & HTML5. O’Reilly, 2018.