Several things:
– Inside a “script” block you do not use <?php, it is all assumeb to be PHP
– You are defining a function named “myfunction”, but the code will not be executed until the function is invoked. And then the variable “$src” is local to the function at the moment.
– note addition of various ; and other stuff.
– note that within double quote strings that php variable are replaced.
What you presumably need is more like, (NB: this may not work but is an improvement) :
<script>
$src = "needs to be set somehow";
function myfunction() {
global $src;
if(1+1=2) {
$src= "myimg2.png";
} else {
$src= "myimg.png";
}
};
</script>
<?php myfunction(); ?>
<img width="60" class="image2" src="$src" />
-
This reply was modified 5 years, 11 months ago by
RossMitchell.
In case someone has a problem like this one, i’ve been testing some things and the solution is this:
<script>
function myfunction() {
if(1+1=2) {
document.getElementById("image2").src= "myimg2.png";
} else {
document.getElementById("image2").src= "myimg.png";
} };
</script>
<img width="60" src="" />
-
This reply was modified 5 years, 11 months ago by
bcworkz. Reason: code fixed