helpmehelpmehelpme Posted December 19, 2011 Share Posted December 19, 2011 I'm trying to write a script that allows the user to select their personal settings for the site. They can pick the color of their background, link color, link hover color and the header color. the first file is supposed to use the defaults, blue for the links, orange for the link hover color red for the background and blue for the header. the second file is supposed to configure the cookie variables to store the values entered by the user and the third file i'm supposed to modify the internal stylesheet to utilize the values stored in the session variables. I tried to set each of the values in file two but i think i messed it up. I dont know how to modify the stylesheet to accept and change to the values of the cookies...Please help here are the files file1.php <?php //Handle the form if it has been submitted: if (isset($_POST['background_color'], $_POST['link_color'], $_POST['link_hover_color'], $_POST['header_color'])){ //send the cookies: setcookie('background_color', $_POST['background_color'], time()+10000000); setcookie('link_color', $_POST['link_color'], time()+10000000); setcookie('link_hover_color', $_POST['link_hover_color'], time()+10000000); setcookie('header_color', $_POST['header_color'], time()=10000000); //Message to be printed later: $msg = '<p>Your settings have been entered! Click <a href="file2.php">here</a> to see them in action.</p>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File1</title> <style type="text/css"> <!-- These are the styles the session (if it exists) should configure --> a:link {color:blue;} a:hover {color:orange;} body {background-color:red} h1 {color:blue; text-align:center;} </style> </head> <body> <?php //if the cookies were sent print a message if (isset($msg)){ print $msg; } ?> <div><p>Please complete this form to configure the appearance of this website:</p> <form action="File2.php" method="post"> <select name="background_color"> <option value="">Background Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="link_color"> <option value="">Link Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="link_hover_color"> <option value="">Link Hover Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="header_color"> <option value="">Header Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <input type="submit" name="submit" value="Configure!" /> </form> </div> </body> </html> file2.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File2</title> </head> <body> <div> <?php include('file1.php'); //check for a background color if (isset($_COOKIE['background_color'])){ print "\body: #" . htmlentities($_COOKIE['background_color']) . ";\n"; }else{ print "\body: #c00"; } //check for a link_color if (isset($_COOKIE['link_color'])){ print "\a:link: #" . htmlentities($_COOKIE['link_color']) . ";\n"; }else{ print "\a:link: #00f"; } //check for a link_hover color: if (isset($_COOKIE['link_hover_color'])){ print "a\:hover: #" . htmlentities($_COOKIE['link_hover_color']) . ";\n"; } //Check for a header color if (isset($_COOKIE['header_color'])){ print "\h1: #" . htmlentities($_COOKIE['header_color']). ";\n"; } ?> Your settings have been updated. <a href="File3.php">Click here</a> to continue. <br /><br /> </div> </body> </html> file3.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File3</title> <style type="text/css"> <!-- These are the styles the session should configure --> a:link {color:blue;} a:hover {color:orange;} body {background-color:red} h1 {color:blue; text-align:center;} </style> </head> <body> <div> <h1>Welcome to YOUR pretty website</h1> <a href = "File1.php">Click here</a> to go back and start over!</a> </div> </body> </html> I'm pretty sure i did in file2 what i was supposed to do in file3 im totally confused Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/ Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 Hmm, this is just a rough idea, but I am fairly certain that all file2 does is print out the data in the cookie. But in file3 something like this should work: <?php $linkcolor = $_COOKIE["link_color"]; // Define the variable ?> <style type="text/css"> <!-- These are the styles the session should configure --> a:link {color:#<?php $linkcolor; ?>} //Use the variable in the style a:hover {color:orange;} body {background-color:red} h1 {color:blue; text-align:center;} </style> Just do the same for all of them. (Also see if it works with and without file2) Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299402 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 I tried that instead of it applying the changes to the styles, it prints returns \body: #c00\a:link: #00f Your settings have been updated. Click here to continue. on the page. I don't know, there must be an error somewhere heres the updated code file1.php <?php //Handle the form if it has been submitted: if (isset($_POST['background_color'], $_POST['link_color'], $_POST['link_hover_color'], $_POST['header_color'])){ //send the cookies: setcookie('background_color', $_POST['background_color'], time()+10000000); setcookie('link_color', $_POST['link_color'], time()+10000000); setcookie('link_hover_color', $_POST['link_hover_color'], time()+10000000); setcookie('header_color', $_POST['header_color'], time()+10000000); //Message to be printed later: $msg = '<p>Your settings have been entered! Click <a href="file2.php">here</a> to see them in action.</p>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File1</title> <style type="text/css"> <!-- These are the styles the session (if it exists) should configure --> a:link {color:blue;} a:hover {color:orange;} body {background-color:red} h1 {color:blue; text-align:center;} </style> </head> <body> <?php //if the cookies were sent print a message if (isset($msg)){ print $msg; } ?> <div><p>Please complete this form to configure the appearance of this website:</p> <form action="File2.php" method="post"> <select name="background_color"> <option value="">Background Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="link_color"> <option value="">Link Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="link_hover_color"> <option value="">Link Hover Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="header_color"> <option value="">Header Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <input type="submit" name="submit" value="Configure!" /> </form> </div> </body> </html> file3.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File3</title> <?php $backgroundcolor = $_COOKIE['background_color']; $linkcolor = $_COOKIE['link_color']; // Define the variable $linkhovercolor = $_COOKIE['link_hover_color']; $headercolor = $_COOKIE['header_color']; ?> <style type="text/css"> <!-- These are the styles the session should configure --> a:link {color:#<?php $linkcolor; ?>} //Use the variable in the style a:hover {color:#<?php $linkhovercolor; ?>} body {background-color:#<?php $backgroudcolor; ?>} h1 {color:#<?php $headercolor; ?> text-align:center;} </style> </head> <body> <div> <h1>Welcome to YOUR pretty website</h1> <a href = "File1.php">Click here</a> to go back and start over!</a> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299413 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 sorry, file 2 returned that but when you click click here to see it in action which goes to file3, it says Notice: Undefined index: background_color in C:\xampp\htdocs\xampp\PHP\file3.php on line 10 Notice: Undefined index: link_color in C:\xampp\htdocs\xampp\PHP\file3.php on line 11 Notice: Undefined index: link_hover_color in C:\xampp\htdocs\xampp\PHP\file3.php on line 12 Notice: Undefined index: header_color in C:\xampp\htdocs\xampp\PHP\file3.php on line 13 Welcome to YOUR pretty website Click here to go back and start over! and the original styles are displayed Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299419 Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 Yeah that may have been my fault. Take out '//Use the variable in the style ' (I forgot to out it in the php tag) and use an " instead of ' for calling the cookie. I have found them to be more reliable $_COOKIE["background_color"] Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299422 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 like this? <style type="text/css"> <!-- These are the styles the session should configure --> a:link {color:#<? $_COOKIE['link_color']; ?>} //Use the variable in the style a:hover {color:#<? $_COOKIE['link_hover_color']; ?>} body {background-color:#<? $_COOKIE['background_color']; ?>} h1 {color:#<? $_COOKIE['header_color']; ?> text-align:center;} </style> it still doesnt change the styles on the page....im confused Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299423 Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File3</title> <?php $backgroundcolor = $_COOKIE["background_color"]; $linkcolor = $_COOKIE["link_color"]; // Define the variable $linkhovercolor = $_COOKIE["link_hover_color"]; $headercolor = $_COOKIE["header_color"]; ?> <style type="text/css"> <!-- These are the styles the session should configure --> a:link {color:#<?php $linkcolor; ?>} a:hover {color:#<?php $linkhovercolor; ?>} body {background-color:#<?php $backgroudcolor; ?>} h1 {color:#<?php $headercolor; ?> text-align:center;} </style> </head> <body> <div> <h1>Welcome to YOUR pretty website</h1> <a href = "File1.php">Click here</a> to go back and start over!</a> </div> </body> </html> Like this. Just copy and paste and post any errors if you get any Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299424 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 no errors this time, but it's still not changing the styles on the page. The background is white, it should change when the user selects a color and goes to file3 Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299429 Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 Slight changes to file1.php. If this does not work, then I am sorry, do not know what to do: <?php //Handle the form if it has been submitted: if (isset($_POST['submit'])){ //send the cookies: setcookie('background_color', $_POST['background_color'], time()+10000000); setcookie('link_color', $_POST['link_color'], time()+10000000); setcookie('link_hover_color', $_POST['link_hover_color'], time()+10000000); setcookie('header_color', $_POST['header_color'], time()+10000000); //Message to be printed later: $msg = '<p>Your settings have been entered! Click <a href="file3.php">here</a> to see them in action.</p>'; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File1</title> <style type="text/css"> <!-- These are the styles the session (if it exists) should configure --> a:link {color:blue;} a:hover {color:orange;} body {background-color:red} h1 {color:blue; text-align:center;} </style> </head> <body> <?php //if the cookies were sent print a message if (isset($msg)){ print $msg; } ?> <div><p>Please complete this form to configure the appearance of this website:</p> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <select name="background_color"> <option value="">Background Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="link_color"> <option value="">Link Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="link_hover_color"> <option value="">Link Hover Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <select name="header_color"> <option value="">Header Color</option> <option value="FF6600">Orange</option> <option value="CC00CC">Purple</option> <option value="FFFF00">Yellow</option> <option value="00FF00">Green</option> <option value="FF0066">Pink</option> <option value="000099">Blue</option> </select> <input type="submit" name="submit" value="Configure!" /> </form> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299431 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 it still doesn't work, but could you do an if statement. like if the user selects whatever color from the drop down list, change the styles color or something like that Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299435 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 i have this example, but im not sure how to do it for the background color and the others, if (isset($_COOKIE['font_color'])) { print "\t\tcolor: #" . htmlentities($_COOKIE['font_color']) . ";\n"; } else { print "\t\tcolor: #000;"; } Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299440 Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 It should work if you copy and paste this for file3.php: <?php $backgroundcolor = $_COOKIE["background_color"]; $linkcolor = $_COOKIE["link_color"]; // Define the variable $linkhovercolor = $_COOKIE["link_hover_color"]; $headercolor = $_COOKIE["header_color"]; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File3</title> <style type="text/css"> <!-- These are the styles the session should configure --> a:link {color:#<?php echo "$linkcolor"; ?>;} a:hover {color:#<?php echo "$linkhovercolor"; ?>;} body {background-color:#<?php echo "$backgroud_color"; ?>;} h1 {color:#<?php echo "$headercolor"; ?>; text-align:center;} </style> </head> <body> <div> <h1>Welcome to YOUR pretty website</h1> <a href = "File1.php">Click here</a> to go back and start over!</a> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299448 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 okay, it looks like they all work except for the background color and the link color. Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299451 Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 okay, it looks like they all work except for the background color and the link color. Sorry stupid spelling mistake by me, here, should all work now: <?php $background_color = $_COOKIE["background_color"]; $linkcolor = $_COOKIE["link_color"]; // Define the variable $linkhovercolor = $_COOKIE["link_hover_color"]; $headercolor = $_COOKIE["header_color"]; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title>File3</title> <style type="text/css"> <!-- These are the styles the session should configure --> a:link {color:#<?php echo "$linkcolor"; ?>;} a:hover {color:#<?php echo "$linkhovercolor"; ?>;} body {background-color:#<?php echo "$background_color"; ?>;} h1 {color:#<?php echo "$headercolor"; ?>; text-align:center;} </style> </head> <body> <div> <h1>Welcome to YOUR pretty website</h1> <a href = "File1.php">Click here</a> to go back and start over!</a> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299453 Share on other sites More sharing options...
helpmehelpmehelpme Posted December 19, 2011 Author Share Posted December 19, 2011 awesome...thanks Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299455 Share on other sites More sharing options...
melloorr Posted December 19, 2011 Share Posted December 19, 2011 awesome...thanks Sorry it took so long (I'm quite tired) Quote Link to comment https://forums.phpfreaks.com/topic/253486-cookies/#findComment-1299460 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.