Re: how to implement AJAX to html [message #174855 is a reply to message #174809] |
Wed, 13 July 2011 19:32 |
pittendrigh
Messages: 4 Registered: December 2010
Karma:
|
Junior Member |
|
|
On Jul 11, 4:51 am, motix <mo...@o2.pl> wrote:
> Hi guys, i am quite new with AJAX, i tried to implement this code --->http://www.outcut.de/MooFlow/example-ajax.htmlin different ways, but
> i can not make it work..Could anyone possibly explain what should i do
> to make it work? thanks a lot:)
Just to get Jerry to jiggle a bit (it's always fun to watch what
happens) here's some simple PHP and ajax mushed together.
mkdir slideshow
....copy some images into slideshow
make a file named nextimage.php that has the following contents:
<?php
session_start();
$arrname = $_GET['arr'];
$_SESSION['cnt'] += 1;
if($_SESSION['cnt'] > count($_SESSION[$arrname])-1)
$_SESSION['cnt'] = 0;
echo $_SESSION[$arrname][$_SESSION['cnt']];
?>
make an index.php that has the following contents:
<?php
session_start();
function mk_rand_seed()
{
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}
function is_image($file)
{
$ret=FALSE;
$dotpos = strrpos($file,'.');
if($dotpos)
{
$suffix = substr($file,$dotpos+1);
if(stristr($suffix,'jpg')
|| stristr($suffix,'jpeg')
|| stristr($suffix,'gif')
|| stristr($suffix,'png')
){ $ret=TRUE;}
}
return $ret;
}
function getImageFilenames($dir) {
$slideshowFilenames = array();
$LclCnt = 0;
if ($dir_handle = opendir($dir)) {
while (($file = readdir($dir_handle)) != false) {
if ($file[0] != '.' && is_image($file)) {
$slideshowFilenames[$LclCnt] = $file;
$LclCnt++;
}
}
closedir($dir_handle);
} else {
echo "failed to opendir $dir for some reason<br>";
exit();
}
return $slideshowFilenames;
}
if(!isset($_SESSION['slideshowFilenames']))
{
$_SESSION['slideshowFilenames'] = getImageFilenames("slideshow");
$_SESSION['cnt'] == count($_SESSION['slideshowFilenames'])-1;
}
echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>MRB Slideshow</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" /
>
<script type="text/javascript">
var http = createRequestObject();
function createRequestObject()
{
var ro;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
ro = new ActiveXObject("Microsoft.XMLHTTP");
}else{
ro = new XMLHttpRequest();
}
return ro;
}
function startMeUp()
{
http.open(\'get\', \'nextimage.php?arr=slideshowFilenames\');
http.onreadystatechange = handleResponse;
http.send(null);
setInterval("mkRequest()", 3500);
}
function mkRequest()
{
http.open(\'get\', \'nextimage.php?arr=slideshowFilenames
\');
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse()
{
if(http.readyState == 4)
{
var response = http.responseText;
var obj = document.getElementById(\'foo\');
obj.style.background = \'url(slideshow/\' + response +
\') no-repeat center center\';
}
}
</script>
';
mk_rand_seed();
$def = $_SESSION['slideshowFilenames']
[rand(0,count($_SESSION['slideshowFilenames']))-1];
echo '
<style>
#foo {
width: 400px; height: 300px;
background: url(slideshow/'.$def.') no-repeat center
center;
}
</style>
</head>
<body onload="startMeUp()">
<div id="foo"></div>
</body>
</html>';
|
|
|