1

I want to pass javascript variable as a path to <img src="path"> in php
my code is
javascript code

    function showSmallImage(path)
    {
        document.getElementById("small_image").value = path;
    }

i am getting path and i have to pass it to
php code

echo "<img id='small_image' src='".$path."'>";
but i am not getting any way to solve it please suggest me any way to solve this issue.
Thanks in advanced!!!
M Gaidhane
  • 531
  • 8
  • 29
  • Well you are aware that javascript is client side code and it's variables / values are in the clients browser. PHP is done server side and doesn't have any access to this kind of stuff. – rfoo Sep 15 '13 at 07:18
  • OK I'm confused - do you want to take a path you have in PHP and echo it on the page or do you want to take the path from an existing and pass that along to PHP? – Deryck Sep 15 '13 at 07:19
  • i have two product image thumbnail and small and path of both images is stored in database. now on click of thumbnail image i want to show small image in a div – M Gaidhane Sep 15 '13 at 07:24
  • so i create a javascript function and call it onclick of thumbnail image and passed path of small image so i am getting that path of small image in function – M Gaidhane Sep 15 '13 at 07:27

3 Answers3

2

Value is not any attribute for image tag and if you want to chnage the sorce if image then use given code

function showSmallImage(path)
    {
        $("#small_image").prop('src',path);
    }
S. S. Rawat
  • 5,943
  • 4
  • 43
  • 59
1

Add this to your HTML file in the [head] section:

<script type="text/javascript">
    var path = "<?php echo $path_from_php; ?>";
</script>

I stole this from another stack answer here. @Subhash posted the other half (javascript side).

Community
  • 1
  • 1
Deryck
  • 7,608
  • 2
  • 24
  • 43
1

You don't need any JavaScript here. Have something like this:

echo "<a href='".$big_path."'><img src='".$small_path."' /></a>";

Where small_path is the PHP variable with the small image path and big_path hold the path to big/full image.

This will give you the small image that when clicked, will show the big image.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208