Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

background-color does weird things

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>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading