-1

I just want to change the background color on my simple site. But it's not only non functional, it's changing the background to red, even if i completly delete all coloring, restart XAMMP and delete the browser chache. I'm completly clueless. And yes.. I know that the code is not efficient, but it was my task to do it that way.

<html>
<head><title>Switch</title></head>
<body>
<p><div id="main">
<meta charset="utf-8"/>
<?php
$Uhrzeit = date("H");

echo '<body style="background-color:blue" />'; //Even set the color outside the if-statement

if($Uhrzeit > 6 && $Uhrzeit <= 12){
    echo '<body style="background-color:blue" />';
}
elseif($Uhrzeit > 12 && $Uhrzeit <= 18){
    echo '<body style="background-color:blue" />';
}
elseif($Uhrzeit > 18 && $Uhrzeit < 6){
    echo '<body style="background-color:black" />';
    echo '<body style="color:white" />';
}

$select = $_POST["Monat"];

switch($select){                                            
case "Januar":                                          
    echo $select . " hat 31 Tage";
    break;
case "Februar":
    echo $select . " hat 28 Tage";
    break;
case "März":
    echo $select . " hat 31 Tage";
    break;
case "April":
    echo $select . " hat 30 Tage";
    break;
case "Mai":
    echo $select . " hat 31 Tage";
    break;
case "Juni":
    echo $select . " hat 30 Tage";
    break;
case "Juli":
    echo $select . " hat 31 Tage";
    break;
case "August":
    echo $select . " hat 31 Tage";
    break;
case "September":
    echo $select . " hat 30 Tage";
    break;
case "Oktober":
    echo $select . " hat 31 Tage";
    break;
case "November":
    echo $select . " hat 30 Tage";
    break;
default:
    echo $select . " hat 31 Tage";
    break;
}
?>
<br><br><br><br><br>
<input type="submit" href="#" onclick="history.back()" value="Zurück">
</div>
</p>
</body>
</html>
RetSIN
  • 13
  • 3
  • 1
    Your first echo of the body is missing a ";" char at the end. And why printing the body tag before your if statements? the `` tag can only be once in an HTML document and it should not be self-closed like `

    `. It should have your page content inside. Check at a [minimal HTML5 code template](https://www.sitepoint.com/a-minimal-html-document-html5-edition/) to understand what I mean. Also, what happens if you load the page without a POST? `$select = isset($_POST['Monat']) ?? '';` would remove the PHP error.

    – Patrick Janser Nov 16 '21 at 08:20

1 Answers1

1

There should be only one body tag in the HTML document. You are printing many, so that's the issue. Instead of echo '<body...' you could store the color in a variable and print it in main body tag.

Example:

<?php

$bgColor = 'blue';

if(something) {
    $bgColor = 'red';
} else if (something) {
    $bgColor = 'yellow';
}

?>
<html>
<head>
<title>Switch</title>
</head>
<body style="background-color: <?php echo $bgColor; ?>">

...

</body>
</html>

Or if you have short_open_tag enabled in php config, you could replace <?php echo $bgColor; ?> with <?=$bgColor;?>

Adrian Kokot
  • 2,172
  • 2
  • 5
  • 18
  • Thank you! I'll do it that way in the future, but it doesn't fix that the background-color is constantly red. (Even if I don't have any red coloring) – RetSIN Nov 16 '21 at 08:32
  • If you follow my advices, it should work. If not, the red probably comes not from the code that you provided. – Adrian Kokot Nov 16 '21 at 08:34