ethereal1m Posted January 19, 2010 Share Posted January 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/189020-how-to-change-background-color-using-php-variable/ Share on other sites More sharing options...
gwolgamott Posted January 19, 2010 Share Posted January 19, 2010 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.";"; Quote Link to comment https://forums.phpfreaks.com/topic/189020-how-to-change-background-color-using-php-variable/#findComment-997989 Share on other sites More sharing options...
ShadowIce Posted January 19, 2010 Share Posted January 19, 2010 <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> Quote Link to comment https://forums.phpfreaks.com/topic/189020-how-to-change-background-color-using-php-variable/#findComment-997992 Share on other sites More sharing options...
gwolgamott Posted January 19, 2010 Share Posted January 19, 2010 try something with echo or print I meant btw <?php echo (' <body bgcolor=" '.$bg;.' " text=" '. $fg;.' "> '); ?> Quote Link to comment https://forums.phpfreaks.com/topic/189020-how-to-change-background-color-using-php-variable/#findComment-997994 Share on other sites More sharing options...
machinatus Posted January 19, 2010 Share Posted January 19, 2010 <body bgcolor="<?php echo $bg; ?>" text="<?php echo $fg; ?>"> Or: <body bgcolor="<?=$bg; ?>" text="<?=$fg; ?>"> You just can't combine "<?php echo" and "<?=". Quote Link to comment https://forums.phpfreaks.com/topic/189020-how-to-change-background-color-using-php-variable/#findComment-997995 Share on other sites More sharing options...
ShadowIce Posted January 19, 2010 Share Posted January 19, 2010 um, try READING harder no offense. I did NOT combine <?php echo?> and <?= Quote Link to comment https://forums.phpfreaks.com/topic/189020-how-to-change-background-color-using-php-variable/#findComment-997997 Share on other sites More sharing options...
Adam Posted January 19, 2010 Share Posted January 19, 2010 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= .. Quote Link to comment https://forums.phpfreaks.com/topic/189020-how-to-change-background-color-using-php-variable/#findComment-997998 Share on other sites More sharing options...
ethereal1m Posted January 20, 2010 Author Share Posted January 20, 2010 Thanks, I just want to kiss you all! Is there any thank you button? Quote Link to comment https://forums.phpfreaks.com/topic/189020-how-to-change-background-color-using-php-variable/#findComment-998481 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.