View Full Version : PHP Issues - Forms and Cookies


BadOP
04-06-2006, 10:47 PM
I'm trying to write a color scheme chooser for my site and its not quite working. Baiscally there's 3 files, "css.php", "foot.php", and "scheme.php". I'm not sure what's going on because it looks correct, but it won't work correctly.

css.php (css.php is included with the include function in the head tags of every page that's part of the main site.):

<?
if (isset($_COOKIE['colorscheme'])) {
$scheme = $_COOKIE['colorscheme'];
if ($scheme = "blue") {
echo "<link href=\"css/blue.css\" rel=\"stylesheet\" type=\"text/css\">";
}
if ($scheme = "red") {
echo "<link href=\"css/red.css\" rel=\"stylesheet\" type=\"text/css\">";
}
}
else {
echo "<link href=\"css/blue.css\" rel=\"stylesheet\" type=\"text/css\">";
}
?>


foot.php:

<div align="center">
&copy; Copyright 2006 BadOP.net<br>
<form method="post" action="scheme.php">
<select name="scheme">
<option name="blue" value="blue">Blue (Default)</option>
<option name="red" value="red">Red</option>
</select>
<input type="submit" name="scheme" value="Select"><br>
</form>
</div>


scheme.php:

<?
setcookie("colorscheme", $_POST['value'], time()+2678400, "/", "badop.net");
header("Location: http://badop.net/");
?>

Douglas
04-07-2006, 12:04 AM
Change scheme.php to this:


<?
setcookie("colorscheme", $_POST['scheme'], time()+2678400, "/", "badop.net");
header("Location: http://badop.net/");
?>


The name of the select thing isn't value it is scheme ;)

BadOP
04-07-2006, 12:09 AM
Doesn't quite work, when I select "red" from the dropdown menu, it'll change red and that part works, but if I go to select blue again it just stays red.

Douglas
04-07-2006, 12:12 AM
Try removing the name="" part from the options.

BadOP
04-07-2006, 12:15 AM
Still didn't work. (Same 'error')

Douglas
04-07-2006, 12:24 AM
Remove the name="" from the button, that should get rid of the error, because when I look through my cookies, the value of it, is Select, which means it overode the name="scheme" on the select box, to the button, because its name is also scheme ;)

BadOP
04-07-2006, 12:31 AM
Still doing it :-\

Douglas
04-07-2006, 12:37 AM
On your site I noticed you didn't have the value="" parameter on the options, you need those for it to work, you just dont need the name="" parameter ;)

BadOP
04-07-2006, 12:56 AM
Even with value in there, it does the samething.

Douglas
04-07-2006, 01:03 AM
I think in your index file you already have a file linked, because when I look at the source before and after the cookie is set, it looks like this:

before:

<link rel="stylesheet" type="text/css" href="css/blue.css" />

after:

<link rel="stylesheet" type="text/css" href="css/blue.css" /><link rel="stylesheet" type="text/css" href="css/red.css" />

BadOP
04-07-2006, 01:06 AM
<head>
<title>BadOP.net</title>
<? include "css.php"; ?>
</head>

Theres my <head></head> tags. It's like that on all content pages..

Douglas
04-07-2006, 01:09 AM
Ok, I think I've got it:

scheme.php


<?
setcookie("colorscheme", $_POST['scheme'], time()+2678400, "/", "badop.net");
header("Location: http://badop.net/");
?>


foot.php


<div align="center">
&copy; Copyright 2006 BadOP.net<br>
<form method="post" action="scheme.php">
<select name="scheme">
<option value="blue">Blue (Default)</option>
<option value="red">Red</option>
</select>
<input type="submit" value="Select"><br>
</form>
</div>


css.php


<?
if (isset($_COOKIE['colorscheme'])) {
$scheme=$_COOKIE['colorscheme'];
if ($scheme == "blue") {
echo "<link href=\"css/blue.css\" rel=\"stylesheet\" type=\"text/css\">";
}
else {
echo "<link href=\"css/red.css\" rel=\"stylesheet\" type=\"text/css\">";
}
}
else {
echo "<link href=\"css/blue.css\" rel=\"stylesheet\" type=\"text/css\">";
}
?>


Try it now ;)

BadOP
04-07-2006, 01:33 AM
I just renamed the name="scheme" line with the submit button to "submit" and it worked, thanks for all of your help.

Douglas
04-07-2006, 01:46 AM
*coughs* told you to do that a while ago *uncoughs* No problem ;)

BadOP
04-07-2006, 02:18 AM
I must've misread then, but all is good. :)