This is not possible because you mix client-side behavior with server-side behavior. What you need to do is create an AJAX request to the server.
If you used a library like jQuery (which you really want, because AJAX does it a breeze), you will do something like this:
PHP code (maybe randomImages.php?)
// query for all images // $_GET['limit'] will have the limit of images // since we passed it from the Javascript // put them all in an array like this: $images = array('images/20.11.A1B9.jpg','images/20.11.A1B9.jpg',...); print json_encode($images); // return them to the client in JSON format. exit;
Client side Javascript
function getRandomImages(limit) {
Alternatively, if the number of images is limited, you can skip all this madness and just have an array with all the images and generate 8 random ones from Javascript itself. This is likely to be better for small datasets and even some larger ones.
source share