Escolar Documentos
Profissional Documentos
Cultura Documentos
John Coggeshall
Welcome!
Who am I: John Coggeshall
Sr. Technical Consultant, Zend Technologies Author PHP 5 Unleashed Zend Educational Advisory Board Speaker on PHP-related topics worldwide Geek
#2
#3
Fair Warning
Ill warn you right now I work for Zend, not Netscape
I am not a client-side developer I do not know which browsers support which constructs of
Javascript under which conditions using which technologies on which operating system I am a PHP developer responsible for scaling numerous mission-critical PHP sites and technologies I do understand Internet architectures and how to scale them in practical environments I do understand enough about AJAX as a technology to speak intelligently
#4
The basics
So, what does AJAX stand for anyway?
Asynchronous Javascript and XML
#5
Asynchronous Javascript
AJAX allows us to take advantage of the server for
information, while leaving the GUI-related items to the client Its not a new technology Just has a neat acronym now Hows it work? Javascript applications perform requests to the server using
an agreed protocol The server responds in kind with the requested information All of this takes place without reloading the page
Asynchronous of the client
#6
Is AJAX Ruby-on-Rails?
No
AJAX is simply the idea of enabling your browser to communicate asynchronously with the server to provide a more rich user Web 2.0 experience.
#7
Implementing AJAX
Step 1: Open a asynchronous connection from
the client to the server Step 2: Perform a request against the server using an agreed upon protocol Step 3: Process the results via Javascript and manipulate the client without causing a full refresh of the page
<SCRIPT language="JavaScript"> <!-pic1= new Image(100,25); pic1.src="http://example.com/getRandomImage.php"; //--> </SCRIPT>
#8
Traditional AJAX
Despite the misconceptions on what exactly
AJAX is, it does have a traditional approach
XMLHttpRequest object
Available in most modern browsers Identical in concept to the Image object
#9
Traditional AJAX
Okay, so here we go:
<input type=text size=5 onBlur=updateCityState()> Now all we need to do is implement a javascript updateCityState() function that creates an XMLHttpRequest object Then we take that object and request a PHP page http://www.example.com/getCityState.php?zip=14214 parse the result update the city and state input fields to reflect the new information!
# 10
My solution: Google
This problem has been solved a million times over so I
wont re-explain the wheel here
# 11
Establishing a Protocol
Now that youve made a request back to the
web server (in this case, using PHP and HTTP GET) time to deal with the response
This is where things really go amuck
# 12
Establishing a Protocol
While there are no standards per-se, there are
common techniques
Future versions of PHP will support JSON encoding by
default Allows you to pass complex data types back and forth between PHP and Javascript fairly easily You can download JSON encoding support from PECL
http://pecl.php.net/package/json $json_enc = json_encode(array(1,2,3)); $json_dec_var = json_decode({ abc:12 }); javascript:eval({ abc:12}); // return foo.abc in JS
# 13
http://www.phpit.net/article/ajax-php-withoutxmlhttprequest/2/
# 14
# 15
# 16
# 18
# 19
# 20
# 21
# 22
# 23
# 24
# 25
# 26
# 27
# 28
Can really mess up your application Bad news: None of the current AJAX toolkits
account for this latency
# 29
# 30
# 31
# 32
# 33
# 34
# 35
# 36
# 37
A thought
# 38
Thank you!
Questions?