'Change background image php loop [closed]

I have a folder of images; I would like to create a page that lists each of them, and clicking them displays that image as the background. I have got it working using

<img .... onclick='document.body.style.backgroundImage = url('img/1.jpg')' >

But when I put that into a FOREACH loop it doesn't work.

foreach($phpfiles as $phpfile)
{
    echo "<img .... onclick='document.body.style.backgroundImage = url(".$phpfile.")' />";
}

Hope someone can help. Only thing I've found mentioned something about Javascript closures, but I couldn't follow the example it had



Solution 1:[1]

Perhaps you forgot to include the path of each image file inside the loop.

foreach($phpfiles as $phpfile)
{
    echo "<img .... onclick='document.body.style.backgroundImage = url('img/".$phpfile."')' />";
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 J A