javascript - Use php function in img "src" tag -
i want instead of specify file name manually, load files dynamicly function return string file name.i have function in php, show code -
<a > <figure> <img src=\"images/parfumes/car.png\" class=\"parfume$productid\" value=\"$productprice \" height=\"225\" width=\"225\"> <figcaption >Цена: $productprice лв.</figcaption> </figure> </a>";
and function return string:
function getppicture($id) { $path = 'images'; $files = scandir($path); foreach ($files $file) { if (is_file($file)) { echo "Файлът несъществува. Моля опитатайте отново!!"; } else { $ext = basename($file, '.*'); if ($ext == $id) { return $ext . ""; break; } } } }
and want that:
<img src=\"images/parfumes/getppicture(somedigit)\" class=\"parfume2\" value=\"$productprice\" height=\"225\" width=\"225\"> <figcaption >$productprice.ЛВ</figcaption> </figure>";
any ideas, correct syntax??
this can accomplished in 2 steps :
- assign id
img
tag (not necessary more convenient)
<img src="images/parfumes/getppicture(somedigit)" id="myimg"class="parfume2" value="$productprice" height="225" width="225">
- change
src
attribute of image tag using javascript
document.getelementbyid('myimg').src = "images/parfumes/" + getppicture(somedigit);
Comments
Post a Comment