• Resolved danielm04

    (@danielm04)


    Hello there, i need help with my code, in the tryit editor on w3shcools it works, but when i paste it on the php block on my website, the website shows me a critical error, can anyone help me with this?

     <script>
          function myfunction() {
        if(1+1=2) { 
            <?php $src= "myimg2.png"?>
    } 
        else {
            <?php $src= "myimg.png"?>
         } 
    };
        </script>
        <img width="60" class="image2" src="<?php echo $src?>" />
    • This topic was modified 4 years, 5 months ago by danielm04.
    • This topic was modified 4 years, 5 months ago by danielm04.
Viewing 2 replies - 1 through 2 (of 2 total)
  • 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 4 years, 5 months ago by RossMitchell.
    Thread Starter danielm04

    (@danielm04)

    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 4 years, 5 months ago by bcworkz. Reason: code fixed
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Php critical error’ is closed to new replies.