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

Link to comment
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.";";

Link to comment
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= ..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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