john6384 Posted July 6, 2007 Share Posted July 6, 2007 Hello, I am able to use variables in my css with the help of php. Now what I need to do is get a form working on my page that changes the stylesheet so that the colour scheme of the page is changed. This is the code for my style sheet (style.php): <?php header("Content-type: text/css"); $colorIn = $_POST['color']; if($colorIn == "green") { $color = "green"; header("Location: index.php"); } else if($colorIn == "uberlord") { $color = "uberlord"; header("Location: index.php"); } echo <<<CSS /* --- start of css --- */ #contentLYR { position:absolute; width:705px; height:800px; z-index:1; border:thin solid 000000; left: 137px; top: 245px; background:$color; } #Border { position:absolute; width:680px; height:780px; z-index:1; left: 10px; top: 10px; background:$color; } ................ /* --- end of css --- */ CSS; ?> And this is in my scheme changer page (index.php): <form method="post" action="style.php"> <input type="radio" name="color" value="green" checked="checked">Green <br> <input type="radio" name="color" value="uberlord">Uberlord <br> <input type="submit" name="submit" value="Change" /> </form> Please advise why this does not work. Thanks anyone Quote Link to comment Share on other sites More sharing options...
calabiyau Posted July 6, 2007 Share Posted July 6, 2007 why not just have the form on index.php send the post data back to itself, then within the head of index.php chose which style sheet to link to according to the result of the post data? and of course have a default selection if the post is empty, which it will be the first time someone accesses the page. Put something like this at the top of index.php <head> <title>Change css style sheet within php page</title> <?php $empty = 0; if (empty($_POST['color'])) { echo '<link rel="stylesheet" type="text/css" href="http://www.phpfreaks.com/forums/Themes/default/style.css?/>'; $empty = 1; } if ($empty != 1) { if ($_POST['color']=='red') { echo '<link rel="stylesheet" type="text/css" href="http://www.phpfreaks.com/forums/Themes/default/style.css?/>'; } if ($_POST['color']=='blue') { echo '<link rel="stylesheet" type="text/css" href="http://www.phpfreaks.com/forums/Themes/default/style.css?/>'; } } ?> </head> Then just have your form like before on the index.php page and set it's target to itself Quote Link to comment Share on other sites More sharing options...
john6384 Posted July 7, 2007 Author Share Posted July 7, 2007 Cheers for the reply. I will be using this idea for my website but not copying the code obviously. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 7, 2007 Share Posted July 7, 2007 You should use a session variable to store what style sheet to use. Posting the color they want for EVERY SINGLE PAGE could get really annoying and you'll have a ton of code at the top of every page. $_SESSION["stylesheet"] = "screen.css" or $_SESSION["stylesheet"] = "screen_green.css" would be MUCH better. Then, all you need is a simple echo statement to put the string in the style sheet declaration. And you can't use PHP in a CSS file. It will render the code. I've tried that before. Well, not render, but the code will just be plain text for the most part. Nothing will happen. Quote Link to comment 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.