Is it possible to let the user choose the background color with php, an
example would be nice.
Thx in advance
Johan
The Netherlands
> 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
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...
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
> 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
>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/
> 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
> $_SESSION["bgcolor"] =
> $_POST["colorr1"].$_POST["colorr1"].$_POST["colorr2"].$_POST["colorg1"].$_POST["colorg2"].$_POST["colorb1"].$_POST["colorb2"];
BTW, you added $_POST["colorr1"] two times. :-)
Matthias
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...
>
>BTW, you added $_POST["colorr1"] two times. :-)
It's my age...either that or seeing double is my normal state
>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
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
"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03060...@posting.google.com...
Why, by refreshing the frameset, of course...
Cheers,
NC
"Nikolai Chuvakhin" <n...@iname.com> wrote in message
news:32d7a63c.03061...@posting.google.com...
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
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...
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
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...
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
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
"Johan" <lijf...@xs4all.nl> wrote in message
news:3eed4c0e$0$49109$e4fe...@news.xs4all.nl...