Jump to content

Can I Dynamically Change Background Color With Php?


jimgilbert

Recommended Posts

I want the bacground of div class "feedback" to change to red if there is feedback (error). Can anyone tell me how to do it with or inside PHP? I tried embedding the javascript but that doesn't work apparently.If ($_SESSION['feedbackmessage'] != "") div.feedback.background="red";is essentially what I want to accomplish. (with correct syntax of course)Thanks in advance,Jim

Link to comment
Share on other sites

Something like this:

<?php	$color = '#0000ff';	if ($_SESSION['feedbackmessage'] != "") {		$color = '#ff0000';	}?>BLAHBLAHBLAH<style type="text/css">	div.feedback {		background-color: <?php echo $color ?>;	}</style>

Link to comment
Share on other sites

Perfect! and easy also - once you know how to do it, as usual. Thanks again DD!I also appreciate your link to the CSS drop-down menu. I was looking for something like that but hadn't found anything yet. Of course, now I have to rethink my menu setup. :)So far I am impressed with what I can do with CSS and PHP. This is fun. My experience up to now was database apps on local networks. Been doing that for almost 20 years and finally decided it is time to migrate to the internet. It is fun for me to learn new stuff like this. Thanks for being here and helping all us novices! Oh, another little(?) question - I see a mix of using classes and ids when referring to divs and I am curious what the difference is. So far I have been using classes, but it appears that the results are the same either way. Are there reasons to use one over the other?Jim

Link to comment
Share on other sites

Oh, another little(?) question - I see a mix of using classes and ids when referring to divs and I am curious what the difference is. So far I have been using classes, but it appears that the results are the same either way. Are there reasons to use one over the other?
The big difference is specificity. If multiple selectors apply to an element, the definitions in the most specific selector "win." ID is the most specific style-sheet selector. So I might have a class selector with maybe 10 style rules that gets applied to most of my paragraphs. But maybe I have a special paragraph that needs just one difference. I assign it to the same class as the others, but I also have an ID selector that has one rule for color or something. Because ID is more specific, that one definition overrules the same definition in the class rules.That's the way good designers use the ID selector. But I notice a lot of people using ID selectors all over the place, which causes them to have a lot of redundant code, since they lose the cascading inheritance effect.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...