benjahnee Posted March 13, 2013 Share Posted March 13, 2013 Hello guys I have tried to create a css style switcher using php. The code shows no errors but when i click a link to change css style on the page, it doesnt direct to the correct place. coukd somebdy take a look at my code and tell me how to change it, i am very new to this so if you could change it for me and explain i would be very grateful. thanks <code> <?php session_start(); ?> <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <body> </head> <?php if(isset($_GET['css'])){ switch ($_GET['css']) { case 'red': $stylesheet = '<link rel="stylesheet" type="text/css" href="alternate.css">'; $_SESSION['switchcss']=$stylesheet; break; case 'yellow': $stylesheet = '<link rel="stylesheet" type="text/css" href="css/main.css">'; $_SESSION['switchcss']=$stylesheet; break; case 'black': $stylesheet = '<link rel="stylesheet" type="text/css" href="css/alternate2.css">'; $_SESSION['switchcss']=$stylesheet; break; // Our default stylesheet default: $stylesheet = '<link rel="stylesheet" type="text/css" href="css/main.css">'; $_SESSION['switchcss']=$stylesheet; } } ?> <?php echo ($_SESSION['switchcss'])? $_SESSION['switchcss']: '<link href="css/main.css" type="text/css" rel="stylesheet">';?> <a href="css/alternate.css<?php echo $_SERVER['PHP_SELF'].'?css=red'; ?>">[red]</a> | <a href="css/main.css<?php echo $_SERVER['PHP_SELF']. '?css=default'; ?>">[blue]</a> | <a href="css/main.css<?php echo $_SERVER['PHP_SELF']. '?css=yellow';?>">[yellow]</a> | <a href="css/alternate2.css<?php echo $_SERVER['PHP_SELF']. '?css=black';?>">[black]</a> </body> </html> <code> Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/ Share on other sites More sharing options...
cyberRobot Posted March 13, 2013 Share Posted March 13, 2013 Have you looked at the source code for this? <a href="css/alternate.css<?php echo $_SERVER['PHP_SELF'].'?css=red'; ?>">[red]</a> Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/#findComment-1418367 Share on other sites More sharing options...
benjahnee Posted March 13, 2013 Author Share Posted March 13, 2013 yes is that some sort of clue? Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/#findComment-1418375 Share on other sites More sharing options...
cyberRobot Posted March 13, 2013 Share Posted March 13, 2013 What is the href attribute currently set to? Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/#findComment-1418377 Share on other sites More sharing options...
benjahnee Posted March 13, 2013 Author Share Posted March 13, 2013 <link href="css/main.css" type="text/css" rel="stylesheet">';?> this one? it is my default style sheet Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/#findComment-1418378 Share on other sites More sharing options...
cyberRobot Posted March 13, 2013 Share Posted March 13, 2013 I'm actually talking about the source code that results from the following line of code: <a href="css/alternate.css<?php echo $_SERVER['PHP_SELF'].'?css=red'; ?>">[red]</a> The answer to your question should be found there. Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/#findComment-1418381 Share on other sites More sharing options...
benjahnee Posted March 13, 2013 Author Share Posted March 13, 2013 im sorry i am very new to this. could you please explain how i must change it thanks very much Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/#findComment-1418382 Share on other sites More sharing options...
cyberRobot Posted March 13, 2013 Share Posted March 13, 2013 Let's break down the code. In the href attribute, you're telling the link to go to the CSS file ("css/alternate.css"). But then you also tack on the actual page address with PHP_SELF. So if your page is called about.php and it's in the root directory, the href attribute would look something like this: "css/alternate.css/about.php?css=red" Does that look correct? First off, I would recommend staying away from PHP_SELF in this case for security reasons. More information can be found here: http://www.cyberscorpion.com/2012-03/why-php_self-should-be-avoided-when-creating-website-links/ You could use the page name, for example. Also, the path to the CSS file shouldn't be included since that's handled by your GET variable ("css"). If your page is named about.php, you would do something like this: <a href="about.php?css=red">[red]</a> Note that you'll need to change the other links too. Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/#findComment-1418384 Share on other sites More sharing options...
benjahnee Posted March 13, 2013 Author Share Posted March 13, 2013 Thanks very much cyber robot i have more or less fixed it with that advice. thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/275597-css-style-switcher-with-php/#findComment-1418389 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.