0

I have this HTML tag (body):

<body onload="startTime()" background="src/bg3.png">

and I was trying to change background on refresh so I replaced the HTML tag with this PHP code:

<?php
    $id = rand(1, 7);
    echo "<body onload=".'"'."startTime()".'"'."background=".'"'."src/bg". $id . ".png".'"'.'>';
?>

which returns:

<body onload="startTime()" background="src/bg5.png">

where the number after bg is a random between 1 and 7.

But the problem is that when I inspect element the body tag is simply:

<body>

Does PHP only allow HTML code insertion if it's being returned by a function?

Don't Panic
  • 37,589
  • 9
  • 55
  • 71
  • `background` should be part of the `style` attribute. You can manipulate HTML with PHP all day long, there is nothing special about it. – Jay Blanchard Apr 24 '17 at 15:44
  • 1
    "But the problem is that when I inspect element the body tag is simply" — That suggests there are either multiple body tags or something is removing the attributes after the page loads. – Quentin Apr 24 '17 at 15:44
  • why do you want to use `php` for this..It can easily be done with some client side scripting languages..eg javascript.. – Lal Apr 24 '17 at 15:44
  • 1
    "Does php only allow html code insertion if it's being return by a function?" — No. – Quentin Apr 24 '17 at 15:44
  • @Lal — It can be easily done with server side languages. Why would you want to use client side languages? (jQuery is not a client side language, it is a JavaScript library). – Quentin Apr 24 '17 at 15:45
  • @Quentin I just meant Javascript..It would be easier to do the above functionality using javascript..I suppose.. – Lal Apr 24 '17 at 15:46
  • 1
    @Lal — It wouldn't be easier. It might seem that way if you are more confident writing JS than PHP. – Quentin Apr 24 '17 at 15:47
  • Chances are that the page is being cached in the browser which prevents you from seeing small changes. Try hitting -F5 to force refresh. If this helps you should add headers to your page to prevent caching. More info on that here [How to prevent Browser cache for php site](http://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site) – Thakkie Apr 24 '17 at 15:47
  • When you ask a question about an error **ALWAYS** include the **error log**. Add `error_reporting(E_ALL); ini_set('display_errors', 1);` at the top of your `php` script, what does it return? – Pedro Lobito Apr 24 '17 at 15:49
  • @Lal yep, that did it, used document.body.background = path; (Answer below) – Daniel Leandro Apr 24 '17 at 17:10

3 Answers3

0

seems you have some problem with quotes ..

<?php
    $id = rand(1, 7);
    echo "<body onload='startTime()' background='src/bg". $id . ".png'>";
?>
scaisEdge
  • 124,973
  • 10
  • 73
  • 87
0

Try:

echo "<body onload='startTime()' style='background:url('src/bg{$id}.png')>";

Your sting was hard to follow but I think you messed up the concatenation.

Background is part of a style tag. I made the Id part of string interpolation rather than more concates and putting single quotes in double works.

nerdlyist
  • 2,654
  • 2
  • 16
  • 26
0

Decided to try using a function again (already tried but didn't work):

    function changeBG() {
        //generate a random number from min to max
        var min = 1, max = 7;
        var str1 = "src/bg", str2 = ".png"
        var number = Math.floor(Math.random() * max) + min;
        var path = str1.concat(number);
        path = path.concat(str2);
        document.body.background = path;
    }

This solved it, thanks to everybody that helped.