How to change background color dynamically in php

I have created website theme. In this I get confused on adding one feature to this. That is I want to change theme background color according to selected color values from user side. For example our table like this:

+------+------------------+---------+
| s_no |    attribute     |  color  |
+------+------------------+---------+
| 1    | background-color | #F6F4F0 |
+------+------------------+---------+

And now I want to set CSS property like this

body{
    background-color:"color value from table"
}

Is this possible? If possible, Kindly explain. Thanks for advance!.

dachi

1,59410 silver badges15 bronze badges

asked Mar 4, 2014 at 12:03

5

You could have a look at this.

//www.barelyfitz.com/projects/csscolor/

It clearly explains how to add CSS style dynamically.

You can do something like this in your PHP


body {
 background:;
 color:;
}
h2, h2, h3, h4 {
 color:;
}
blockquote {
 color:;
}

answered Mar 4, 2014 at 12:06

BKMBKM

6,6796 gold badges29 silver badges45 bronze badges

2

You can use PHP in Styles like

body{
    background-color:
}

And while getting the $color each time use RAND[] function.

answered Mar 4, 2014 at 12:05

GautamD31GautamD31

28.2k10 gold badges60 silver badges84 bronze badges

3




answered Mar 4, 2014 at 12:08

Instead of write PHP in your CSS you can use javascript [ajax request]. I have not enough information about what you want to do but you can retrieve data by using AJAX when the user choose a value in the select. It's more complex to develop but write PHP code into CSS... Isn't dirty ? Maybe I'm wrong on this point.

answered Mar 4, 2014 at 12:14

DebflavDebflav

1,1317 silver badges17 bronze badges

Try this javascript function....


function changeBG[color]
{
var body=document.getElementsByTagName['body'][0];
body.style.background= color;
}

and fire this function on button click or any event you want like..

Chủ Đề