Escolar Documentos
Profissional Documentos
Cultura Documentos
Ajax Tutorial
Ajax Tutorial
18 de agosto de 2006
AJAX - Tutorial
Agenda
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Introducao
Sincrono X Assncrono
O que e Ajax?
Para que Serve?
Exemplo 1 - Carregando um Arquivo
ajaxInit
Metodos e Atributos
Exibindo o Resultado
Exemplo 2 - Calculadora
Exemplo 3a - Alimentacao de Select
Json
Exemplo 3b - Alimentacao de Select com Json
Sugestao de Conte
udo
Conclusao
Referencias
AJAX - Tutorial
Sncrono X Assncrono!
Sncrono
I
I
I
Assncrono
I
I
I
AJAX - Tutorial
O que e AJAX
AJAX - Tutorial
Produzir aplicac
oes WEB mais semelhantes as Desktop.
AJAX - Tutorial
Tradicional X AJAX
http://adaptivepath.com/images/publications/essays/ajaxfig1.png
http://adaptivepath.com/images/publications/essays/ajaxfig2.png
AJAX - Tutorial
C
odigo Fonte 1: ajaxInit.js
1
function
a j a x I n i t () {
var xmlhttp ;
3
4
try {
x m l h t t p = new XMLHttpRequest ( ) ;
} catch ( ee ) {
try {
x m l h t t p = new A c t i v e X O b j e c t ( " Msxml2 . XMLHTTP " ) ;
} catch ( e ) {
try {
x m l h t t p = new A c t i v e X O b j e c t ( " Microsoft . XMLHTTP " ) ;
} catch (E) {
xmlhttp = f a l s e ;
}
}
}
5
6
7
8
9
10
11
12
13
14
15
16
17
18
return xmlhttp ;
19
20
AJAX - Tutorial
Exemplo 1
AJAX - Tutorial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
AJAX - Tutorial
Metodos e Atributos
I
Metodos
I
Atributos
I
0
1
2
3
4
(uninitialized);
(a carregar);
(carregado);
(interactivo);
(completo).
AJAX - Tutorial
Exibindo os Resultados
AJAX - Tutorial
Calculadora
http://200.201.81.38/anselmo/ciclo/exemplo2.html.
AJAX - Tutorial
function calcular () {
ajax = a j a x I n i t () ;
document . g e t E l e m e n t B y I d ( " resultado " ) . innerHTML = "
Calculando ... " ;
i f ( ajax ) {
ajax . onreadystatechange = resultado ;
v1 = document . g e t E l e m e n t B y I d ( " v1 " ) . v a l u e ;
v2 = document . g e t E l e m e n t B y I d ( " v2 " ) . v a l u e ;
u r l = http : / / 2 0 0 . 2 0 1 . 8 1 . 3 8 / anselmo / ciclo /
calcular . php ? v1 = +v1+& v2 = +v2 ;
a j a x . open ( GET , u r l , t r u e ) ;
a j a x . send ( n u l l ) ;
}
}
13
14
15
16
17
18
19
20
function resultado () {
i f ( a j a x . r e a d y S t a t e == 4 ) {
i f ( a j a x . s t a t u s == 2 0 0 ) {
document . g e t E l e m e n t B y I d ( " resultado " ) .
innerHTML = a j a x . r e s p o n s e T e x t ;
}
}
}
AJAX - Tutorial
<?
e c h o $ GET [ v1 ] + $ GET [ v2 ] ;
2
3
?>
AJAX - Tutorial
AJAX - Tutorial
<?
h e a d e r ( " Content - Type : text / html ; charset = iso -8859 -1 " ) ;
3
4
5
6
7
8
9
10
11
$ e s t a d o = $ GET [ estado ] ;
$ c i d a d e s = "" ;
i f ( $ e s t a d o==" pr " ) {
$ c i d a d e s .= " < option value =\"1\" > Tup~
a ssi </ option > " ;
$ c i d a d e s .= " < option value =\"2\" > Toledo </ option > " ;
$ c i d a d e s .= " < option value =\"3\" > Cascavel </ option > " ;
$ c i d a d e s .= " < option value =\"4\" > Pato Branco </ option > " ;
}
12
13
14
15
16
17
i f ( $ e s t a d o==" sp " ) {
$ c i d a d e s .= " < option value =\"5\" > Mogi </ option > " ;
$ c i d a d e s .= " < option value =\"6\" > Palmeiras </ option > " ;
$ c i d a d e s .= " < option value =\"7\" > Santos </ option > " ;
}
18
19
20
echo $ c i d a d e s ;
?>
AJAX - Tutorial
10
11
12
13
14
15
16
17
function escreveCidades () {
i f ( a j a x . r e a d y S t a t e == 4 ) {
i f ( a j a x . s t a t u s == 2 0 0 ) {
document . g e t E l e m e n t B y I d ( " cidade " ) . innerHTML =
ajax . responseText ;
}
}
}
18
19
< s e l e c t name=" estado " i d=" estado " onChange=" if ( this . options
[ this . selectedIndex ]. value ) { s e l ec i o n a C id ad e ( this . options
[ this . selectedIndex ]. value ) } ">
AJAX - Tutorial
Json
http://www.json.org/
AJAX - Tutorial
<?
h e a d e r ( " Content - Type : text / html ; charset = iso -8859 -1 " ) ;
3
4
5
6
7
8
9
10
11
$ e s t a d o = $ GET [ estado ] ;
$ c i d a d e s = " { cidades :[ " ;
i f ( $ e s t a d o==" pr " ) {
$ c i d a d e s .= " { cdg : 1 , nm : Tup~
a ssi } ,
{ cdg : 2 , nm : Toledo } ,
{ cdg : 3 , nm : Cascavel } ,
{ cdg : 4 , nm : Pato Branco }]} " ;
}
12
13
14
15
16
17
i f ( $ e s t a d o==" sp " ) {
$ c i d a d e s .= " { cdg : 5 , nm : Mogi } ,
{ cdg : 6 , nm : Palmeiras } ,
{ cdg : 7 , nm : Santos }]} " ;
}
18
19
20
echo $ c i d a d e s ;
?>
AJAX - Tutorial
AJAX - Tutorial
function escreveCidadesJson () {
i f ( a j a x . r e a d y S t a t e == 4 ) {
i f ( a j a x . s t a t u s == 2 0 0 ) {
var x = 0;
v a r j s o n = e v a l ( " ( "+a j a x . r e s p o n s e T e x t+" ) " ) ;
l i m p a r D e s t i n o ( " cidade2 " ) ;
f o r ( x =0; x<=j s o n . c i d a d e s . l e n g t h ; x++){
o p t i o n = document . c r e a t e E l e m e n t ( " option " ) ;
o p t i o n . s e t A t t r i b u t e ( " value " , j s o n . c i d a d e s [ x ] .
cdg ) ;
o p t i o n . a p p e n d C h i l d ( document . c r e a t e T e x t N o d e (
j s o n . c i d a d e s [ x ] . nm) ) ;
document . g e t E l e m e n t B y I d ( document .
c r e a t e T e x t N o d e ( j s o n . c i d a d e s [ x ] . nm) ) ;
document . g e t E l e m e n t B y I d ( " cidade2 " ) .
appendChild ( option ) ;
}
}
}
}
AJAX - Tutorial
/
Remove t o d o s o s e l e m e n t o s f i l h o s de um e l e m e n t o
/
function limparDestino ( destino ) {
o b j = document . g e t E l e m e n t B y I d ( d e s t i n o ) ;
while ( obj . f i r s t C h i l d )
obj . removeChild ( obj . f i r s t C h i l d ) ;
}
AJAX - Tutorial
10
11
<?php
# F i l e N a m e=" C o n n e c t i o n _ p h p _ m y s q l . htm "
# Type=" MYSQL "
# HTTP=" true "
$ h o s t n a m e c o n e x a o = " localhost " ;
$ d a t a b a s e c o n e x a o = " test " ;
$ u s e r n a m e c o n e x a o = " aluno " ;
$ p a s s w o r d c o n e x a o = " aluno " ;
$conexao = mysql pconnect ( $hostname conexao ,
$username conexao , $password conexao ) or d i e ( m y s q l e r r o r
() ) ;
m y s q l s e l e c t d b ( $database conexao , $conexao ) ;
?>
AJAX - Tutorial
<?
i n c l u d e ( " conexao . php " ) ;
2
3
$ s e a r c h = $ GET [ s ] ;
4
5
6
7
8
9
10
11
12
13
14
15
$ r e s = s u b s t r ( $ r e s , 0 , ( s t r l e n ( $ r e s ) 1) ) ;
$ r e s .= " ]} " ;
echo $ r e s ;
16
17
18
19
?>
AJAX - Tutorial
Vantagens
AJAX - Tutorial
Problemas
AJAX - Tutorial
Referencias
I
DHTML
http://www.quirksmode.org/js/cross dhtml.html
AJAX - Tutorial