...

View Full Version : php changing color bg code help



Extrovertive
07-13-2002, 10:01 PM
im trying to make a page that change color background by clicking on a link. however, it doesn't work. why?



<?php
if(!isset($_POST[color])) $_POST[color] = 'green';
?>

<html>
<head>
<title>Head</title>
</head>
<body bgcolor=<?php print "$_POST[color]";?>>
Welcome!

<a href="<?php echo $_SERVER[PHP_SELF];?>?color=red">red</a>
<a href="<?php echo $_SERVER[PHP_SELF];?>?color=blue">blue</a>

</body>
</html>


can any show me a more better code to do it? thanks.

chrisvmarle
07-14-2002, 01:51 AM
How about this then?


<?php
if(!isset($color)) $color = 'green';
?>

<html>
<head>
<title>Head</title>
</head>
<body bgcolor=<?php print "$color";?>>
Welcome!

<a href="<?php echo $_SERVER[PHP_SELF];?>?color=red">red</a>
<a href="<?php echo $_SERVER[PHP_SELF];?>?color=blue">blue</a>

</body>
</html>

Extrovertive
07-14-2002, 05:05 AM
doesnt work on my localhost

firepages
07-14-2002, 05:29 AM
Hi, you are sending the variables via the url so you need to use HTTP_GET_VARS i.e.



<?php
if(!isset($_GET[color])) $_GET[color] = 'green';
?>

<html>
<head>
<title>Head</title>
</head>
<body bgcolor=<?php print "$_GET[color]";?>>
Welcome!

<a href="<?php echo $_SERVER[PHP_SELF];?>?color=red">red</a>
<a href="<?php echo $_SERVER[PHP_SELF];?>?color=blue">blue</a>

</body>
</html>

Extrovertive
07-14-2002, 08:26 AM
thank you for helping a php newbie. u are the man.

ErikaT
08-12-2003, 02:43 PM
Can someone update the code so that the site remembers the color you last selected? :)

Erika

Íkii
08-13-2003, 11:50 AM
<?php
session_start();
$_SESSION['color'] = ((isset($_GET['color'])) ? $_GET['color'] : (isset($_SESSION['color']) ? $_SESSION['color'] : 'green')); // default
?>
<html>
<head>
</head>
<body bgcolor=<?php echo $_SESSION['color']; ?>>
etc....
<?php
$colors = array('red','green','blue','yellow','black','white','gold');
foreach($colors AS $value)
{
echo '<a href="?color=' .$value. '">' .$value. '</a> ';
}
?>
</body>
</html>

that probably over complicates things a tad - basically we're just storing the color as a session variable now, which bases its value on the existence of the get variable.
if the ternery (a=b) ? true : false; conditional interests you, there is a tutorial here (http://www.teckis.com/scriptix/tutorials/subpage/ternery/teck.html)
hopefully you'll be able to suss the foreach looping yourself.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum