Calling PHP function using JQuery .ajax() -
i'm trying use php function read server directory images. images need passed jquery using .ajax() use in slideshow. i'm using php function hoping simplify adding/removing images user. basically, they'd have add or remove images directory modify slideshow , not edit code.
i'm struggling jquery function return i'm expecting. it's been while since i've done php may overlooking someone. here's php code:
<?php $path = "./"; while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') echo "<img src=\"" . $path.$file. " alt=\"" . $file . "\>"; { } ?>
here's readimages jquery function:
function readimages() { $.ajax({ type: "post", url: "getimages.php", datatype: "html", success: function(imagepath){ $(".slideshow").html(imagepath); } });
}
what i'm trying php return html formatted tag file name added. jquery adds returned tag html code.
so far... it's returning "; { } ?>
any ideas i'm doing wrong?
the main issue borken if structure rob olmos pointed out. switch directory iterator instead of readfile
if youre using php5.
<?php $path = "./"; $dir = new directoryiterator(realpath($path)); $img = '<img src="%s" alt="%s" />'; foreach($dir $file){ if(!$dir->isdot() && 0 !== strpos('.', $file->getfilename()) && !$file->isdir()){ $filepath = $path . $file->getfilename(); echo sprintf($img, $filepath, $filepath); } } ?>
Comments
Post a Comment