Home > database >  background-color does weird things
background-color does weird things

Time:11-16

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>

CodePudding user response:

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;?>

  • Related