Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/58702-change-css-style-sheet-within-php-page/
Share on other sites

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

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.