thecard Posted December 8, 2008 Share Posted December 8, 2008 I have an <img src="file.php" /> So this php file sets it's headers to look like an image and displays an image. But would it be possible for this php file to also carry out some javascript as well? I have tried echoing a link to a javascript file like this inside file.php: echo"<script src='js.js'></script>"; But I can see why this doesn't work..I think because once the headers are set inside the php file to be an image, it can't echo anything, and so can't even echo a link to javascript! How can I make a php file, "fired" through an <img> tag set off javascript functions from within the php file? ??? Thanks for any help. thecard Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/ Share on other sites More sharing options...
premiso Posted December 8, 2008 Share Posted December 8, 2008 echo"<script src='js.php'></script>"; Just rename it .php and it should work fine. Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-709833 Share on other sites More sharing options...
thecard Posted December 9, 2008 Author Share Posted December 9, 2008 Thanks, I'll try that when I get home. So I must rename the .js file to .php? Why might this make it work? I thought the reason it didn't was because I couldn't echo anything inside the .php file because it had img headers. As I said, I'll try it tonight. thecard Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710467 Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 Thanks, I'll try that when I get home. So I must rename the .js file to .php? Why might this make it work? I thought the reason it didn't was because I couldn't echo anything inside the .php file because it had img headers. As I said, I'll try it tonight. thecard You could link java.textfile as long as it prints out actual javascript. You are telling the tag <script that the file should be type text/javascript, or you should I guess: echo "<script type='text/javascript' src='js.php'></script>"; Although it is not needed, best practice. The above is common practice cause sometimes you want to dynamically create a javascript with custom data etc from your script, so the above would allow you to use php to gather data then print out javascript and have it work all hunky dory. Where as the .js does not allow for this functionality, that means the file will not change unless you manually change it. EDIT: I just read the original post more closely, sorry you were asking if you can run the javascript inside of an image file, I do not think this is possible due to the fact the image is a binary type and adding txt to it will kill it/make it not display. So the short answer would be no. Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710536 Share on other sites More sharing options...
thecard Posted December 9, 2008 Author Share Posted December 9, 2008 Well that's annoying. Is there some other way to execute javascript FROM the image? Link it to another .php etc.? I can't work it out Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710645 Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 Well that's annoying. Is there some other way to execute javascript FROM the image? Link it to another .php etc.? I can't work it out Describe how you want it to be used, why do you need to linked to javascript? Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710647 Share on other sites More sharing options...
thecard Posted December 9, 2008 Author Share Posted December 9, 2008 The javascript will carry out AJAX -> php mysql querys which must be asynchronous Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710677 Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 The javascript will carry out AJAX -> php mysql querys which must be asynchronous What would this javascript send back to the server? What I am getting at, is why does it need to be JS in the image? What data are you trying to gather from it, and why can you not just put the query update inside the file.php? Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710685 Share on other sites More sharing options...
thecard Posted December 9, 2008 Author Share Posted December 9, 2008 Right: The img tag links to a .php file which loads an image after some mysql requests to make sure which one to load etc. I want the php file to send off this information (the timestamp etc. and more complex stuff) asynchornously to my database, so the best way to do this would be through javascript I think. But how can I link the javascript file with the xmlhttprequest and handler function to the .php file being used as the image? Thanks for your hard work. I assure you I have thought about this for a while without any results! Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710744 Share on other sites More sharing options...
sasa Posted December 9, 2008 Share Posted December 9, 2008 .php file can directly save information to database you don't need to send it to browser and back to server Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710755 Share on other sites More sharing options...
thecard Posted December 9, 2008 Author Share Posted December 9, 2008 Asynchrounosly? Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710759 Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 Asynchrounosly? You do not need asyncrounosly. When the file.php is called do your DB calls before the image is display, then print out the image. No need for js to do that, it will do it on the backend before the page is loaded. Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710784 Share on other sites More sharing options...
thecard Posted December 9, 2008 Author Share Posted December 9, 2008 You misunderstand me: For special reasons I NEED it to be asynchronous. It has to keep updating until the user leaves the page. Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710795 Share on other sites More sharing options...
premiso Posted December 9, 2008 Share Posted December 9, 2008 I do not think what you want is possible with the image file...why not just include it in the actual page instead of the image? So the answer to your question, no it is not possible. Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710834 Share on other sites More sharing options...
thecard Posted December 9, 2008 Author Share Posted December 9, 2008 OK. Its fine really, just would have been nice. I can just do it with another link Thanks sooo much for all that help. thecard Quote Link to comment https://forums.phpfreaks.com/topic/136122-solved-a-php-file-forced-through-an-to-fire-off-javascript-function/#findComment-710853 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.