I've implemented changing src of an image using javascript but I don't want any javascript in my website now. So I need help to do that in PHP! :)

What am I doing:

In the main index.php file,

<body onload="load_images();">

Then I include a random file

include("content_to_display.php");

At the end of content_to_display.php,

<script type='text/javascript'>
function load_images()
{
  document.getElementById('pic').setAttribute('src','/resources/resource/features/old_old_website/preview.png' );
}
</script>

and each image has an id

<img src="blank.jpg" id="pic"/>

How can I complete change that javascript into a PHP code?

Please help! I tried searching on google without much help.

Recommended Answers

All 7 Replies

Your post is very unclear
you cant change the source of an image after page load in php
the image will only be different each page load, will not cycle through images while the page is open
What do you want to accomplish

Your post is very unclear
you cant change the source of an image after page load in php
the image will only be different each page load, will not cycle through images while the page is open
What do you want to accomplish

Hey! Remember what you suggested in the other thread? After a page loads change the source of the image?

I want to do the exact same thing but in PHP and not javascript. Simply put, I need to do this

<script type='text/javascript'>
function load_images()
{
  document.getElementById('IMG001').setAttribute('src','the_path_to_the_actual_image.png' );
}
</script>

in PHP.

Member Avatar for rajarajan2017
<?php
  echo "<img src=\"$path\" title=\"$title\" alt=\"$alt\" />"; 
?>
<?php
  echo "<img src=\"$path\" title=\"$title\" alt=\"$alt\" />"; 
?>

No, thats not what I meant, I could have just written the <img/> simply if I wanted to do that.. what I am doing is changing the source of the image after page loads to reduce the time it takes for the text to show up.

So I have a <img/> tag where the source is a random picture file. Then the page loads, text shows up for the user to read. NOW, its time for all the random pictures to show up. So I use that javascript...

This is the same as me putting an image on the page.

The other thread used javascript,
php serverside
javascript clientside
nothing in php occurs after the page source is downloaded to the browser
to change the page source after loading is done in javascript
an image randomizer can be done in php and that random image name written into the javascript, so the image is different each page refresh
but anything after the page is served is javascript
Is this what you want, an php image randomizer?

php serverside
javascript clientside
nothing in php occurs after the page source is downloaded to the browser
to change the page source after loading is done in javascript

Well said, almostbob. Not to get off topic, but they should really sticky a thread about the difference between server-side and client-side scripting, so people stop confusing the capabilities of each. I was starting to write one but I don't know if it'll just get lost in the forum...

Oh, I see. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.