Darkmatter5 Posted October 30, 2008 Share Posted October 30, 2008 I have a page that has this Javascript at the top. <script type="text/javascript"> <!-- var imageURL="<?php echo $filename; ?>"; if(document.images) { var graph=new Image(); graph.src=filename; var blank=new Image(); blank.src="images/blank_graph.png"; } function changeImage(filename) { if(document.images) { if(imageURL=="images/blank_graph.png") imageURL=filename; else imageURL="images/blank_graph.png"; document.myImage.src=imagesURL; } } //--> </script> And down in the page is PHP that produces this code: echo "<a onClick='changeImage('$filename');'>$values[year]</a>"; Now as you can see the intent is for this link to change an image to another image if clicked. But I need to send the dynamic filename to the Javascript. How can I do this? Here is the code for the image that will be changed. <img src="images/blank_graph.png" name="graph"> BTW here's where I got the code for the Javascript: http://www.sislands.com/coin70/week3/onclick.htm Quote Link to comment Share on other sites More sharing options...
webster08 Posted October 31, 2008 Share Posted October 31, 2008 should work just the way you have it. you could also could pass it with ajax; if the page (with the js and html) does not have a php file extension. Quote Link to comment Share on other sites More sharing options...
BoltZ Posted October 31, 2008 Share Posted October 31, 2008 I had tried to pass php in javascript. Javascript is client-side and PHP is server-side so it won't work, at least it never did for me. Try setting that variable in javascript also I guess. You would have to set the extra variable in each document but that won't take long. I'm sure other people here have better suggestions then that though Quote Link to comment Share on other sites More sharing options...
Darkmatter5 Posted October 31, 2008 Author Share Posted October 31, 2008 I finally got it completely working, with PHP variables passed to Javascript. Here's the code that works. <script type="text/javascript"> <!-- var imageURL="images/blank_graph.png"; if(document.images) { var graph=new Image(); graph.src="images/graphs/" + filename + ".png"; var blank=new Image(); blank.src="images/blank_graph.png"; } function changeImage(filename) { if(document.images) { imageURL="images/graphs/" + filename + ".png"; document.graph.src=imageURL; } } //--> </script> Here's the onClick event: echo "<a onClick='changeImage($row1[folder_id]$minyear)'> $minyear</a>"; Enjoy! Quote Link to comment Share on other sites More sharing options...
BoltZ Posted October 31, 2008 Share Posted October 31, 2008 Well you didn't really pass a php variable in the <script> tag. That was what I was talking about. But congrats on fixing the problem Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.