Jump to content

how to change background color using php variable


ethereal1m

Recommended Posts

Dear experts,

how can I change the background color using php variable?

 

Suppose I have the following code:

<html>
<head><title>Front Door</title></head>
<?php
//$bg = $_COOKIE['bg'];
//$fg = $_COOKIE['fg'];
$bg="red";
$fg="black";
?>

<body bgcolor="<?php=$bg ?>" text="<?php= $fg ?>">

<h1>This is some text</h1>

</body>
</html>

The code hard code bg and fg ph variables to red and black respectively and use those to set the html body background and foreground colors. However the code doesn't work, in this case it sets the background to black instead of red. Which part that I made a syntax mistake?

 

I try to do this so that latter on I can pass values via cookies.

 

Regards,

Ethereal1m

If using a css style that will do or use that code to change the body background color. Granted $back_color needs to be given that variable from a something like a drop down box or entered in a text box and submited... or using something else like javascript but neither here or there that line should help you some.

echo "background-color:".$back_color.";";

Notice the red "deprecated" warnings on the BODY attributes:

 

http://www.w3schools.com/tags/tag_body.asp

 

As gwolgamott was saying you should use the style attribute, or better yet a CSS class:

 

body {
    background-color: <?php echo $bg; ?>;
    color: <?php echo $fg; ?>;
}

 

 

<body bgcolor="<?php=$bg ?>" text="<?php= $fg ?>">

 

And you were actually combining the two: <?php= ..

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.