Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Change background color

1 view
Skip to first unread message

Johan

unread,
Jun 7, 2003, 8:02:12 AM6/7/03
to
Hello,

Is it possible to let the user choose the background color with php, an
example would be nice.

Thx in advance

Johan
The Netherlands


Matthias Esken

unread,
Jun 9, 2003, 10:29:53 AM6/9/03
to
"Johan" <lijf...@xs4all.nl> schrieb:

> Is it possible to let the user choose the background color with php, an
> example would be nice.

Try this:
-----------------------------------8<-----------------------------------
<?php
$color = isset($_POST["color"]) ? $_POST["color"] : 0;
switch (intval($color)) {
case 1:
$bgcolor = "FF0000";
break;
case 2:
$bgcolor = "00FF00";
break;
case 3:
$bgcolor = "0000FF";
break;
default:
$bgcolor = "FFFFFF";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Background</title>
</head>
<body bgcolor="#<?php echo($bgcolor) ?>">
<form action="<?php echo($_SERVER["PHP_SELF"]) ?>" method="post"
name="form" id="form">
<select name="color" id="bgcolor">
<option value="0" SELECTED>white</option>
<option value="1">red</option>
<option value="2">green</option>
<option value="3">blue</option>
</select>
<input type="submit" name="cmdOK" id="cmdOK" value="OK">
</form>
</body>
</html>
-----------------------------------8<-----------------------------------

Matthias

Johan

unread,
Jun 7, 2003, 10:04:05 AM6/7/03
to
Matthias,

Thx 4 your reply,

Your solution works perfectly, but it works only for one page
I need something that works for the entire domain so it works on all html
documents.

Maybe if it is possible to somehow save the chosen color and use it in the
body background line of every page? Or are there better ways?

Thx

Johan

"Matthias Esken" <muellei...@usenetverwaltung.org> wrote in message
news:bc2cm...@usenet.esken.de...

Andy Chase

unread,
Jun 9, 2003, 9:08:26 AM6/9/03
to
Hi Johan,

Tbis could be accomplished with the use of cookies (which would also allow
the user's choice of background to be set for future visits for as long as
the cookie lasts). It could also be done with sessions.

http://us3.php.net/manual/en/features.cookies.php
http://us3.php.net/manual/en/ref.session.php

Good luck,

-Andy

Matthias Esken

unread,
Jun 9, 2003, 2:52:02 PM6/9/03
to
"Johan" <lijf...@xs4all.nl> schrieb:

> Your solution works perfectly, but it works only for one page
> I need something that works for the entire domain so it works on all html
> documents.

It would be a good idea to store the data in a session. See a working
demo at http://www.kunterbunte.info/color_select/index.php and download
the code at http://www.kunterbunte.info/color_select/color_select.zip.

Groeten,
Matthias

Geoff Berrow

unread,
Jun 9, 2003, 3:49:29 PM6/9/03
to
Message-ID: <bc2s1...@usenet.esken.de> from Matthias Esken contained the
following:

>It would be a good idea to store the data in a session. See a working
>demo at http://www.kunterbunte.info/color_select/index.php and download
>the code at http://www.kunterbunte.info/color_select/color_select.zip.

Why not make the option value the hex value for the colour then
you could have
$_SESSION["bgcolor"] = $_POST["color"];

or even
$_SESSION["bgcolor"] =
$_POST["colorr1"].$_POST["colorr1"].$_POST["colorr2"].$_POST["colorg1"].$_POST["colorg2"].$_POST["colorb1"].$_POST["colorb2"];

http://www.ckdog.co.uk/php/color.php
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

Matthias Esken

unread,
Jun 9, 2003, 4:55:41 PM6/9/03
to
Geoff Berrow <$b...@ckdog.co.uk> schrieb:

> Message-ID: <bc2s1...@usenet.esken.de> from Matthias Esken contained the
> following:
>
>> It would be a good idea to store the data in a session. See a working
>> demo at http://www.kunterbunte.info/color_select/index.php and download
>> the code at http://www.kunterbunte.info/color_select/color_select.zip.
>
> Why not make the option value the hex value for the colour then
> you could have
> $_SESSION["bgcolor"] = $_POST["color"];

Sure, that's OK. I just wanted to give the basic idea. Now just add a
small error check and it's fine.

$defaultcolor = "FFFFFF";
$_SESSION["bgcolor"] = isset($_POST["color"]) ? $_POST["color"] : $defaultcolor;

Even this ist not really safe. It doesn't matter now when we're just
talking about a color value, but I could post any data to your page.

Matthias

Matthias Esken

unread,
Jun 9, 2003, 5:05:17 PM6/9/03
to
Geoff Berrow <$b...@ckdog.co.uk> schrieb:

> $_SESSION["bgcolor"] =
> $_POST["colorr1"].$_POST["colorr1"].$_POST["colorr2"].$_POST["colorg1"].$_POST["colorg2"].$_POST["colorb1"].$_POST["colorb2"];

BTW, you added $_POST["colorr1"] two times. :-)

Matthias

Johan

unread,
Jun 7, 2003, 3:43:29 PM6/7/03
to
Here is an example of what i exactly want

I have 3 pages, index.html + frame1.html + frame3.html

I can allready choose in frame 1 what i want as backcolor and set this
choice in a cookie

Now when i refresh frame2, it reads the cookie for the background color.
(Don't know yet how to do this automatically)

So my question is as follows:

1. How can i refresh frame2 automatically when i change color?
2. It sees the cookie is only saved for the duration of the page is being
used, is there any way to save the color so it is
used next time the page is being called again?
3. Got any hint or tips for the following example i made?

************* Index.html ****************
<HTML>
<HEAD>
<TITLE> bkcolor change </TITLE>
</HEAD>
<FRAMESET FRAMEBORDER="0" FRAMESPACING="0" ROWS="100%" COLS="20%,*">
<FRAME SRC="frame1.html" NAME="frame1" NORESIZE SCROLLING="auto"
MARGINHEIGHT="0" MARGINWIDTH="0">
<FRAME SRC="frame2.html" NAME="frame2" NORESIZE SCROLLING="AUTO"
MARGINHEIGHT="0" MARGINWIDTH="0">
</FRAMESET>
**************************************
************* frame1.html ****************


<?php
$color = isset($_POST["color"]) ? $_POST["color"] : 0;
switch (intval($color)) {
case 1:
$bgcolor = "FF0000";
break;
case 2:
$bgcolor = "00FF00";
break;
case 3:
$bgcolor = "0000FF";
break;
default:

$bgcolor = $_COOKIE['bcolor'];
}
?>
<? Setcookie('bcolor',$bgcolor) ?>


<html>
<head>
<title>Background</title>
</head>
<body bgcolor="#<?php echo($bgcolor) ?>">

<form action="<?php echo($_SERVER["PHP_SELF"]) ?>" method="post"
name="form" id="form">
<select name="color" id="bgcolor">
<option value="0" SELECTED>white</option>
<option value="1">red</option>
<option value="2">green</option>
<option value="3">blue</option>
</select>
<input type="submit" name="cmdOK" id="cmdOK" value="OK">
</form>
</body>
</html>

**************************************
************* frame2.html ****************
<body bgcolor="#<?php Print($_COOKIE['bcolor']) ?>">
</body>
**************************************


"Geoff Berrow" <$b...@ckdog.co.uk> wrote in message
news:qum9evk4a1rrjfq0s...@4ax.com...

Geoff Berrow

unread,
Jun 9, 2003, 5:34:25 PM6/9/03
to
Message-ID: <bc33rd...@usenet.esken.de> from Matthias Esken contained
the following:

>


>BTW, you added $_POST["colorr1"] two times. :-)

It's my age...either that or seeing double is my normal state

Geoff Berrow

unread,
Jun 9, 2003, 5:38:37 PM6/9/03
to
Message-ID: <bc339...@usenet.esken.de> from Matthias Esken contained the
following:

>Sure, that's OK. I just wanted to give the basic idea. Now just add a


>small error check and it's fine.
>
>$defaultcolor = "FFFFFF";
>$_SESSION["bgcolor"] = isset($_POST["color"]) ? $_POST["color"] : $defaultcolor;
>
>Even this ist not really safe. It doesn't matter now when we're just
>talking about a color value, but I could post any data to your page.

Yes, security is something I'm always hazy on, being a bit of a newbie

Nikolai Chuvakhin

unread,
Jun 9, 2003, 8:13:53 PM6/9/03
to
Matthias Esken <muellei...@usenetverwaltung.org> wrote
in message news:<bc2cm...@usenet.esken.de>...
>
> switch (intval($color)) {
> case 1:
> $bgcolor = "FF0000";
> break;
> case 2:
> $bgcolor = "00FF00";
> break;
> case 3:
> $bgcolor = "0000FF";
> break;
> default:
> $bgcolor = "FFFFFF";
> }
> ?>
...

> <body bgcolor="#<?php echo($bgcolor) ?>">

I would suggest chainging the last line to

<body bgcolor="<?php echo $bgcolor; ?>">

This will allow to define a color as either a hexadecimal RGB
number:

$bgcolor = "#FF0000";

or as a named color:

$bgcolor = "Red";

If you hard-code the "#", the latter will not be available...

Cheers,
NC

Johan

unread,
Jun 9, 2003, 2:09:35 PM6/9/03
to
Any ideas of how to automatically refresh the other frames?


"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03060...@posting.google.com...

Nikolai Chuvakhin

unread,
Jun 10, 2003, 11:48:27 PM6/10/03
to
"Johan" <lijf...@xs4all.nl> wrote in message
news:<3ee64022$0$49115$e4fe...@news.xs4all.nl>...

>
> Any ideas of how to automatically refresh the other frames?

Why, by refreshing the frameset, of course...

Cheers,
NC

Johan

unread,
Jun 10, 2003, 9:52:11 AM6/10/03
to
How can i refresh this in code?


"Nikolai Chuvakhin" <n...@iname.com> wrote in message

news:32d7a63c.03061...@posting.google.com...

Nikolai Chuvakhin

unread,
Jun 12, 2003, 5:22:32 PM6/12/03
to
"Johan" <lijf...@xs4all.nl> wrote in message
news:<3ee75550$0$49109$e4fe...@news.xs4all.nl>...

> > >
> > > Any ideas of how to automatically refresh the other frames?
> >
> > Why, by refreshing the frameset, of course...
>
> How can i refresh this in code?

It depends on how you plan to store the background color...
Here's the simplest possible case: no storage, everything
is interactive:

index.php:
<?php

if (isset ($_REQUEST['bgcolor'])) {
$q = '?bgcolor=' . $_REQUEST['bgcolor'];
} else {
$q = '';
}

?>
<html>
<frameset cols="300, *">
<frame name="left" src="left.php<?php
echo $q;
?>" scrolling="no" noresize>
<frame name="right" src="right.php<?php
echo $q;
?>" scrolling="auto">
</frameset>
</html>
[end of index.php]

left.php
<?php

if (isset ($_GET['bgcolor'])) {
$bgcolor = $_GET['bgcolor'];
} else {
$bgcolor = '#FFFFFF';
}

?>
<html>


<body bgcolor="<?php echo $bgcolor; ?>">

<p>This is the left frame... </p>
</body>
</html>
[end of left.php]

right.php
<?php

if (isset ($_GET['bgcolor'])) {
$bgcolor = $_GET['bgcolor'];
} else {
$bgcolor = '#FFFFFF';
}

?>
<html>


<body bgcolor="<?php echo $bgcolor; ?>">

<p>This is the right frame... </p>
</body>
</html>
[end of right.php]

So if you run index.php without arguments, both frames will
have white background. If you want, say, a red background,
you should run index.php?bgcolor=%23FF0000 or simply
index.php?bgcolor=red...

You could also have a form like this anywhere on your Web site:

<form method="POST" action="http://yoursite.com/" target=_top>
<SELECT name="bgcolor">
<option value="#000000">Black</option>
<option value="#C0C0C0">Silver</option>
<option value="#808080">Gray</option>
<option value="#FFFFFF">White</option>
<option value="#FFFF00">Yellow</option>
<option value="#800000">Maroon</option>
<option value="#000080">Navy</option>
<option value="#008000">Green</option>
<option value="#FF0000">Red</option>
<option value="#0000FF">Blue</option>
<option value="#800080">Purple</option>
<option value="#008080">Teal</option>
<option value="#FF00FF">Fuchsia</option>
<option value="#00FFFF">Aqua</option>
<option value="#00FF00">Lime</option>
<option value="#808000">Olive</option>
</SELECT>
<input type="submit" value="Change BG Color!">
</form>

The same story: the frameset receives the new background
color and passes it down to frames... Note that I
deliberately used $_REQUEST in index.php to enable
the transmission of bgcolor via either GET or POST...

Obviously, this is a toy. On a real site, you would
store bgcolor somewhere and then refresh the frameset
without arguments knowing that frames will check their
colors when reloading...

Cheers,
NC

Johan

unread,
Jun 11, 2003, 10:01:08 PM6/11/03
to
Nikolai ,

thx for your reply,

I tried your examples and i can see the bgcolor of the index file is
changing, but still the frames don't change along, maybe if your could take
a look at what have at the moment. Maybe i did not understand you right.

Thx in advance

Johan

******** Index.php ***********
<?php

if (isset ($_REQUEST['bgcolor'])) {

$q = '?bgcolor='.$_REQUEST['bgcolor'];
} else {
$q = '';
}

?>
<html>
<frameset cols="300, *">
<frame name="left" src="left.php<?php echo $q; ?>" scrolling="no" noresize>
<frame name="right" src="right.php<?php echo $q; ?>" scrolling="auto">
</frameset>
</html>

***************************
******** Left.php ***********
<?php


if (isset ($_GET['bgcolor'])) {
$bgcolor = $_GET['bgcolor'];
} else {
$bgcolor = '#FFFFFF';
}

?>
<html>

<body bgcolor="<?php echo $bgcolor; ?>">

<?php echo $bgcolor; ?>

<p>This is the left frame... </p>

<form method="POST" action="http://lijffijt.xs4all.nl/index.php"


target=_top>
<SELECT name="bgcolor">
<option value="#000000">Black</option>
<option value="#C0C0C0">Silver</option>
<option value="#808080">Gray</option>
<option value="#FFFFFF">White</option>
<option value="#FFFF00">Yellow</option>
<option value="#800000">Maroon</option>
<option value="#000080">Navy</option>
<option value="#008000">Green</option>
<option value="#FF0000">Red</option>
<option value="#0000FF">Blue</option>
<option value="#800080">Purple</option>
<option value="#008080">Teal</option>
<option value="#FF00FF">Fuchsia</option>
<option value="#00FFFF">Aqua</option>
<option value="#00FF00">Lime</option>
<option value="#808000">Olive</option>
</SELECT>
<input type="submit" value="Change BG Color!">
</form>

</body>
</html>


***************************
******** Right.php ***********
<?php

if (isset ($_GET['bgcolor'])) {
$bgcolor = $_GET['bgcolor'];
} else {
$bgcolor = '#FFFFFF';
}

?>
<html>
<body bgcolor="<?php echo $bgcolor; ?>">
<p>This is the right frame... </p>
</body>
</html>

***************************


"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03061...@posting.google.com...

Nikolai Chuvakhin

unread,
Jun 15, 2003, 12:45:09 AM6/15/03
to
"Johan" <lijf...@xs4all.nl> wrote in message
news:<3eeb5d7b$0$49116$e4fe...@news.xs4all.nl>...

>
> I tried your examples and i can see the bgcolor of
> the index file is changing, but still the frames
> don't change along

Sorry, forgot to urlencode() the variable... Change this
part of index.php:

> if (isset ($_REQUEST['bgcolor'])) {
> $q = '?bgcolor='.$_REQUEST['bgcolor'];
> } else {
> $q = '';
> }

to look like this:

if (isset ($_REQUEST['bgcolor'])) {

$q = '?bgcolor=' . urlencode ($_REQUEST['bgcolor']);
} else {
$q = '';
}

Check this out:

http://ncbase.com/tmp/
Default white background

http://ncbase.com/tmp/?bgcolor=%230000ff
Blue background specified as #0000ff
(%23 is the URL code for #)

http://ncbase.com/tmp/?bgcolor=Yellow
Yellow background specified by name

Cheers,
NC

Johan

unread,
Jun 12, 2003, 12:52:55 PM6/12/03
to
Nikolai,

Thank you very much, works perfectly now :)

One last question, can you recomend a good book about php?

Thx

Johan


"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03061...@posting.google.com...

Nikolai Chuvakhin

unread,
Jun 15, 2003, 3:29:04 PM6/15/03
to
"Johan" <lijf...@xs4all.nl> wrote in message
news:<3eec2e80$0$49102$e4fe...@news.xs4all.nl>...

>
> Thank you very much, works perfectly now :)

You are very welcome.

> can you recomend a good book about php?

Not really... PHP Manual works really well for me, so
I never had a feeling I needed a book...

Cheers,
NC

Johan

unread,
Jun 13, 2003, 2:01:23 AM6/13/03
to
Where did you get this php manual?

I am really having fun with this php and like to have a good manual or help

"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03061...@posting.google.com...

Tony Marston

unread,
Jun 16, 2003, 6:08:58 AM6/16/03
to
Try http://www.php.net/manual/en/

Tony Marston

"Johan" <lijf...@xs4all.nl> wrote in message

news:3eed4c0e$0$49109$e4fe...@news.xs4all.nl...

0 new messages