tema XMLHttpRequest
Moderator: Moderatori
- black diamond
- Senior Member
- Mesaje: 298
- Membru din: Lun Mai 23, 2005 8:42 pm
- Contact:
index.php
[php]
<html>
<head>
<title>AJAX test</title>
<script type="text/javascript" src="js.js"></script>
</head>
<body>
<p><a href="javascript:sndReq('Kia Ora')">[Kia Ora]</a> |
<a href="javascript:sndReq('Bula')">[Bula]</a> |
<a href="javascript:sndReq('Bonjour')">[Bonjour]</a>
<a href="javascript:sndReq('Valy')">[VAly???]</a></p>
<p> </p>
<div id="foo"> </div>
</body>
</html>
[/php]
js.js
[php]
function createRequestObject() {
var ro;
try {
if (window.XMLHttpRequest) ro = new XMLHttpRequest()
else ro = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(er) {
alert('Ajax is not available for this browser');
}
return ro;
}
var http = createRequestObject();
function sndReq(action) {
http.open('get', 'rpc.php?action='+action);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response.indexOf('|') != -1) {
update = response.split('|');
changeText(update[0], update[1])
}
}
}
function changeText( div2show, text ) {
// Detect Browser
var IE = (document.all) ? 1 : 0;
var DOM = 0;
if (parseInt(navigator.appVersion) >=5) {DOM=1};
if (DOM) {
var viewer = document.getElementById(div2show)
viewer.innerHTML=text
}
else if(IE) {
document.all[div2show].innerHTML=text
}
}
[/php]
rpc.php
[php]
<?php
header("Cache-Control: no-cache");
header("Pragma: nocache");
switch($_REQUEST['action']) {
case 'Kia Ora':
$output = "New Zealand";
break;
case 'Bula':
$output = 'Fiji';
break;
case 'Bonjour':
$output = 'France';
break;
case 'Valy':
$output = 'Valy Rullz';
break;
}
$output = "foo|<font color='navy'>{$output} < < " . time() . ' >> ';
echo $output;
?>
[/php]
opera 9beta, ie 6, firefox 1.5.0.1
[php]
<html>
<head>
<title>AJAX test</title>
<script type="text/javascript" src="js.js"></script>
</head>
<body>
<p><a href="javascript:sndReq('Kia Ora')">[Kia Ora]</a> |
<a href="javascript:sndReq('Bula')">[Bula]</a> |
<a href="javascript:sndReq('Bonjour')">[Bonjour]</a>
<a href="javascript:sndReq('Valy')">[VAly???]</a></p>
<p> </p>
<div id="foo"> </div>
</body>
</html>
[/php]
js.js
[php]
function createRequestObject() {
var ro;
try {
if (window.XMLHttpRequest) ro = new XMLHttpRequest()
else ro = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(er) {
alert('Ajax is not available for this browser');
}
return ro;
}
var http = createRequestObject();
function sndReq(action) {
http.open('get', 'rpc.php?action='+action);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array();
if(response.indexOf('|') != -1) {
update = response.split('|');
changeText(update[0], update[1])
}
}
}
function changeText( div2show, text ) {
// Detect Browser
var IE = (document.all) ? 1 : 0;
var DOM = 0;
if (parseInt(navigator.appVersion) >=5) {DOM=1};
if (DOM) {
var viewer = document.getElementById(div2show)
viewer.innerHTML=text
}
else if(IE) {
document.all[div2show].innerHTML=text
}
}
[/php]
rpc.php
[php]
<?php
header("Cache-Control: no-cache");
header("Pragma: nocache");
switch($_REQUEST['action']) {
case 'Kia Ora':
$output = "New Zealand";
break;
case 'Bula':
$output = 'Fiji';
break;
case 'Bonjour':
$output = 'France';
break;
case 'Valy':
$output = 'Valy Rullz';
break;
}
$output = "foo|<font color='navy'>{$output} < < " . time() . ' >> ';
echo $output;
?>
[/php]
opera 9beta, ie 6, firefox 1.5.0.1
|
p.s. pentru anumite lucruri trebuie un dram de logica si rabdare... sau o cautare scurta pe net. faceti uz de google!
p.s. pentru anumite lucruri trebuie un dram de logica si rabdare... sau o cautare scurta pe net. faceti uz de google!
- oriceon
- PHPRomania Moderator
- Mesaje: 480
- Membru din: Vin Dec 24, 2004 3:48 pm
- Localitate: Constanta
- Contact:
Mersi domnule diamant.
Se pare ca problema mea consta in ...
[php]
header("Cache-Control: no-cache");
header("Pragma: nocache");
[/php]
... pentru ca mie imi scria o singura data ... nu tot timpul ...
Now, seems that`s ok, thanks again.
Un vin si pt codita si pt tine
Se pare ca problema mea consta in ...
[php]
header("Cache-Control: no-cache");
header("Pragma: nocache");
[/php]
... pentru ca mie imi scria o singura data ... nu tot timpul ...
Now, seems that`s ok, thanks again.
Un vin si pt codita si pt tine
.:: Develop and Fun in .Ro Style ::.
I love and i enjoy browsing web with
I love and i enjoy browsing web with
-
carco
- Senior Member
- Mesaje: 2799
- Membru din: Joi Mai 27, 2004 4:36 pm
- Localitate: Bucuresti
- Contact:
Vreau si eu vin!
index.php
[php]
<?='<?xml version="1.0" encoding="utf-8"?>'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple AJAX</title>
<script type="text/javascript">
/*<![CDATA[*/
function scrie() {
var d = new Date();
var bug = document.getElementById('bug');
document.getElementById('wait').style.display='inline';
bug.src='scrie.php?'+d.getTime()+Math.random();
bug.onload=function() {document.getElementById('wait').style.display='none';}
}
/*]]>*/
</script>
</head>
<body>
<p>
<a href="index.php?status=nojavascript" onclick="scrie(); return false;">scrie</a>
</p>
<p>
<img id="bug" src="<?=empty($_GET['status'])?'ok.png':'scrie.php'?>" alt="." />
<small id="wait" style="display:none">asteptati...</small>
</p>
</body>
</html>
[/php]
scrie.php
[php]
<?php
header('Content-Type: image/png');
sleep(3); //comment this
if(!$x=@fopen("test.txt", "a+")) {
readfile('no.png');
return false;
}
if(!@fwrite($x, "a")) {
readfile('no.png');
return false;
}
@fclose($x);
readfile('ok.png');
?>
[/php]
Nota:
- in loc de ok.png/no.png se poate folosi un bug (un gif transparent de 1x1 pixeli)
- Intrebare pentru specialistii in javascript, exista posibilitatea de a vedea cumva ce dimensiune are o imagine care e pusa ca src? (in ideea de a comunica erorile prin imagini de dimensiuni diferite)
index.php
[php]
<?='<?xml version="1.0" encoding="utf-8"?>'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simple AJAX</title>
<script type="text/javascript">
/*<![CDATA[*/
function scrie() {
var d = new Date();
var bug = document.getElementById('bug');
document.getElementById('wait').style.display='inline';
bug.src='scrie.php?'+d.getTime()+Math.random();
bug.onload=function() {document.getElementById('wait').style.display='none';}
}
/*]]>*/
</script>
</head>
<body>
<p>
<a href="index.php?status=nojavascript" onclick="scrie(); return false;">scrie</a>
</p>
<p>
<img id="bug" src="<?=empty($_GET['status'])?'ok.png':'scrie.php'?>" alt="." />
<small id="wait" style="display:none">asteptati...</small>
</p>
</body>
</html>
[/php]
scrie.php
[php]
<?php
header('Content-Type: image/png');
sleep(3); //comment this
if(!$x=@fopen("test.txt", "a+")) {
readfile('no.png');
return false;
}
if(!@fwrite($x, "a")) {
readfile('no.png');
return false;
}
@fclose($x);
readfile('ok.png');
?>
[/php]
Nota:
- in loc de ok.png/no.png se poate folosi un bug (un gif transparent de 1x1 pixeli)
- Intrebare pentru specialistii in javascript, exista posibilitatea de a vedea cumva ce dimensiune are o imagine care e pusa ca src? (in ideea de a comunica erorile prin imagini de dimensiuni diferite)
Programator cu experienta in Magento/ZF, Typo3/Flow3, Symfony, B2B, CRM, ERP, SMB... vand betoniera
- black diamond
- Senior Member
- Mesaje: 298
- Membru din: Lun Mai 23, 2005 8:42 pm
- Contact:
Înapoi la “Librarii Javascript”
Cine este conectat
Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 1 vizitator
