• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 595
  • Last Modified:

Dynamically switch external CSS stylesheets with PHP

I'm using this tutorial
http://www.gr0w.com/articles/code/css_stylesheet_switcher_using_php_javascript_free/
to dynamically switch external CSS stylesheets using PHP.

The code is setup to have the links that switch the css be placed on the same page to which the css is being applied.  I'd like to be able to select the css on one page, and when you click the link, it takes you to a second page, automatically applying the chosen css to that page.

Here are my sample files so far (start at page1.php):
http://caprishine.com/test/

Thanks so much,
-heather :)
0
heathercapri
Asked:
heathercapri
1 Solution
 
lamxingCommented:
It looks like you second button has a duplicated form name with the first button.

 <form id="form1" name="form1" method="post" action="page2.php?SETSTYLE=0">
    <input type="submit" name="Submit" value="View Style 1" />
  </form>
 <form id="form1" name="form1" method="post" action="page2.php?SETSTYLE=1">
    <p>
      <input type="submit" name="Submit2" value="View Style 2" />
    </p>
 </form>


Just change the second form to <form id="form2" name="form2" method="post" action="page2.php?SETSTYLE=1"> and it should work.
0
 
Bernard S.CTOCommented:
The best way to keep your "current" stylesheet from one page to the other is to put it in a cookie or in a session variable.
An example might be:
--- style.php ---
<?php
$my_style=@$_SESSION['style'] . ' '; //needs a session_start() in the including php file - value may be empty.
                                 // at least a space between 2 single quotes
$my_style=trim($my_style); // remove any trailing space
if (strlen($my_style)<1) {$my_style='/path/to/style/dir/default.css'; $_SESSION['style']=$mystyle;};
                         //avoid testing against 0 (common error: testing = instead of ==)
                         //some tuning/trial and error might be needed to get the right value of the file path
echo '<LINK rel="stylesheet" href="' . $my_style .'" type="text/css" >'; //double quote-single quote, then single-double
                        // this echo is probably in a better place if in the including php script
?>
------- page 1 ------
<?php
session_start(); // better to put that just at the top of code
....
echo '<html><head>'; // start outputting html code before the echo in style.php happens
include_once "style.php"; // we now have the current style (in any), otherwise the default one
.....
// get in $my_new_style the filename AND PATH of the new stylesheet
...
// prepare the style switch
$_SESSION['style']=$my_new_style; //  this style will be available at the next page load
....
//rest of the php script / page
?>

------- page 2 ------
<?php
session_start(); // better to put that just at the top of code
....
echo '<html><head>'; // start outputting html code before the echo in style.php happens
include_once "style.php"; // we now have the current style (in any), otherwise the default one
...
//rest of the php script / page
?>

0
 
RoonaanCommented:
You can set cookies using javascript. No need for php to be involved.

-r-
0
 
heathercapriAuthor Commented:
Thanks for the responses...

lamxing - thank you for pointing that out.  I renamed my second form's elements, but that didn't help at all.

fibo - I guess I need to clarify a bit.  I'm not trying to keep my "current" stylesheet from one page to another.  I want a user to be able to select a particular style that is to be applied only to the new page that opens up when they click the link.  I don't care about the style that's on page 1.  But it seems like you know your PHP, so maybe you could still help me with this.  I guess I just want to somehow pass a variable using php that stores the name of the desired css style, and then when page2.php opens up, it executes the script that changes the CSS on that page to whatever the user picked from page1.php.  Does this make sense?  I hope you can help!

Thanks again, everyone!
-heather :)
0
 
RoonaanCommented:
When you don't want to actually store the chosen stylesheet in a cookie, you would just use in page2.php:

<?php
  include_once 'stylearray.php';
  $css = isset($_REQUEST['SETSTYLE']) ? intval($_REQUEST['SETSTYLE']) : 0;
  $sheet = isset($styleSheets[$css]) ? $styleSheets[$css] : $styleSheets[0];

?>
<html>
  <head>
   <?php echo $sheet['sheet'];?>
  </head>

Or do I still don't understand it correctly.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now