Jump to content

Archived

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

ethereal1m

how to change background color using php variable

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

Share this post


Link to post
Share on other sites

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.";";

Share this post


Link to post
Share on other sites
<html>
<head><title>Front Door</title></head>
<?php
//$bg = $_COOKIE['bg'];
//$fg = $_COOKIE['fg'];
$bg="red";
$fg="black";
?>

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

<h1>This is some text</h1>

</body>
</html>

Share this post


Link to post
Share on other sites

try something with echo or print I meant btw

<?php

echo ('

<body bgcolor=" '.$bg;.' " text=" '. $fg;.' ">

');

?>

Share this post


Link to post
Share on other sites

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

 

Or:

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

 

You just can't combine "<?php echo" and "<?=". ;)

Share this post


Link to post
Share on other sites

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= ..

Share this post


Link to post
Share on other sites

×
×
  • 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.