/    Sign up×
Community /Pin to ProfileBookmark

quick template change

Basically what I’m trying to do is have it so when a user selects a certain button it will reload the page using the style sheet that their input selects.

Not sure why this isn’t working, but this is what I’ve got:

[code=php]

<?php

echo “<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>”;
echo “<head><title> {$title} </title>”;

if (“‘$_POST[default]'”)
{
echo “<link rel=”stylesheet” href=”style1.css” type=”text/css”>”;
}
elseif (“‘$_POST[black]'”)
{
echo “<link rel=”stylesheet” href=”style2.css” type=”text/css”>”;
}
elseif (“‘$_POST[purple]'”)
{
echo “<link rel=”stylesheet” href=”style3.css” type=”text/css”>”;
}
elseif (“‘$_POST[white]'”)
{
echo “<link rel=”stylesheet” href=”style4.css” type=”text/css”>”;
}
else
{
echo “There was an error.”;
}

echo “</head>”;

echo “<body>”;

echo “<form action=”index.php”>
<input type=”radio” value=”default” name=”default”>Default</option>
<input type=”radio” value=”black” name=”black”>Black</option>
<input type=”radio” value=”purple” name=”purple”>Purple</option>
<input type=”radio” value=”white” name=”white”>White</option>
<input type=”submit” name=”Change Style” />

</form>”;

echo “<div class=”nav”><a href=”index.php”>Home</a> | <a href=”register.php”>Register</a> | <a href=”links.php”>Links</a> | <a href=”faq.php”>FAQ</a></div>”;

echo “</div>”;

echo “</body></html>”;

?>

[/code]

One of four style sheets:

[code=php]
<?php

echo “<style type=”text/css”>

#forms { position:absolute;top:75px;left:200px;align:right;padding-bottom:5px;}
#text { position:absolute; top:75px; left:125px; align:left;}
.text2 { padding-top:8px;}
.forms2 { padding-bottom:6px;}
#login {position:absolute;top:100px;left:75%;}
.nav{position:absolute; top:75px; left:10%; right:10%; width:80%;background-color:#0099FF;text-align:right;a}
.input{width:100px;} </style>”;

?>

[/code]

Now I know I’m missing something obvious, but could someone please point it out? Thanks. The only thing I can think of is that perhaps the page loads before it can call up the style sheet?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@knowjOct 24.2006 — thats one i wrote for one of my flat mates if thats any help
[code=php]<?
//template selection
if ($_GET['cookie'] == "delete")
{
setcookie ("jw", "", time()-60000, '/', 'www.url');
header("Location: http://www.url");
exit;
}
else if (isset($_GET['cookie'])&&($_GET['cookie'] <=4))
{
setcookie('jw', $cookie, time()+60*60*24*30, '/', 'www.url');
header("Location: http://www.url");
exit;
}
//load the header

if (isset($_COOKIE['jw']))
{
echo '<link href="css/style'.$_COOKIE['jw'].'.css" rel="stylesheet" type="text/css" />';
}
else
{
echo '<link href="css/style1.css" rel="stylesheet" type="text/css" />';
}
?> [/code]
Copy linkTweet thisAlerts:
@BleedauthorOct 24.2006 — ^ thanks but I'm more interested in finding what's wrong with what I've got. ?
Copy linkTweet thisAlerts:
@knowjOct 24.2006 — firstly its a bit messy


theres 2 types of quotes use them wisely (well 3 if you include `)
[code=php]

echo "<style type="text/css">

#forms { position:absolute;top:75px;left:200px;align:right;padding-bottom:5px;}
#text { position:absolute; top:75px; left:125px; align:left;}
.text2 { padding-top:8px;}
.forms2 { padding-bottom:6px;}
#login {position:absolute;top:100px;left:75%;}
.nav{position:absolute; top:75px; left:10%; right:10%; width:80%;background-color:#0099FF;text-align:right;a}
.input{width:100px;} </style>";
[/code]

$data = hello

"$data" would show hello if echoed

'$data' would show $data if echoed

there is no need for the army of

[code=php]
echo '<style type="text/css">
#forms { position:absolute;top:75px;left:200px;align:right;padding-bottom:5px;}
#text { position:absolute; top:75px; left:125px; align:left;}
.text2 { padding-top:8px;}
.forms2 { padding-bottom:6px;}
#login {position:absolute;top:100px;left:75%;}
.nav{position:absolute; top:75px; left:10%; right:10%; width:80%;background-color:#0099FF;text-align:right;a}
.input{width:100px;}
</style>';
[/code]



the same with your actual code ill have a run through it and try to see what the problem is.



also why do you echo everything why don't you just exit the php its just making more work for yourself?

why????? '"$_POST[value]"' what are the quotes for your saying if '"$_POST[value]"' its not putting a value in its litrally putting "$_POST[value]"

the correct format for $_POST[] is $_POST['name']

finally your not putting the variable any ware so as soon as anyone clicks its going to loose the variable and change back to the default style or whatever you have as standard
Copy linkTweet thisAlerts:
@knowjOct 24.2006 — that is what it should look like. i don't know if it compiles as i haven't checked it also theres the fate of you not passing the variable any further than the page so as soon as a link is touched its going to change back you need to use cookies and/or sessions

[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en">
<head>
<title/><?php echo $title; ?></title>

<?php
if ($_POST['style'] == "default")
{
echo '<link rel="stylesheet" href="style1.css" type="text/css"/>';
}
elseif ($_POST['style'] == "black")
{

echo '<link rel="stylesheet" href="style2.css" type="text/css"/>';
}
elseif ($_POST['style'] == "purple")
{
echo '<link rel="stylesheet" href="style3.css" type="text/css"/>';
}
elseif ($_POST['style'] == "white")
{
echo '<link rel="stylesheet" href="style4.css" type="text/css">';
}
else
{
echo 'There was an error.';
}
?>
</head>
<body>
<form action="">
<input type="radio" value="default" name="style">Default</option>
<input type="radio" value="black" name="style">Black</option>
<input type="radio" value="purple" name="style">Purple</option>
<input type="radio" value="white" name="style">White</option>
<input type="submit" name="Change Style"/>
</form>

<!-- ideally this should be a unordered list with display:inline; in the css -->
<div class="nav"><a href="index.php">Home</a> | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>
</div>
</body>
</html>
[/code]



and considering the data your handling and what you wanting to do you should use a switch rather than an army of if, else if, else if, else if etc...

http://uk.php.net/switch

[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en">
<head>
<title/><?php echo $title; ?></title>

<?php

switch ($_POST['style'])
{
case "default":
echo '<link rel="stylesheet" href="style1.css" type="text/css"/>';
break;
case "black":
echo '<link rel="stylesheet" href="style2.css" type="text/css"/>';
break;
case "purple":
echo '<link rel="stylesheet" href="style3.css" type="text/css"/>';
break;
case "white":
echo '<link rel="stylesheet" href="style4.css" type="text/css">';
break;
case 0:
echo '<link rel="stylesheet" href="style1.css" type="text/css"/>';
break;
}
?>
</head>
<body>
<form action="">
<input type="radio" value="default" name="style">Default</option>
<input type="radio" value="black" name="style">Black</option>
<input type="radio" value="purple" name="style">Purple</option>
<input type="radio" value="white" name="style">White</option>
<input type="submit" name="Change Style"/>
</form>

<!-- ideally this should be a unordered list with display:inline; in the css -->
<div class="nav"><a href="index.php">Home</a> | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>
</div>
</body>
</html>
[/code]


but i will say your better off using my code and having your urls ?cookie=1,2,3 etc.... e.g <a href="?cookie=2">black</a>


a final note why i'm at it why is the CSS in an echo its completely unneeded with the style of script your using. you just need standard style sheets in standard .css files
Copy linkTweet thisAlerts:
@SheldonOct 24.2006 — Just a thought, what you have done looks alright, You may want to set a cookie or session when the user selects a style sheet, and you need to add a method to your form.

[code=php]
<?php

session_start();

echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">";
echo "<head><title> {$title} </title>";


if(isset($_SESSION['stylesheet'])){
echo "<link rel="stylesheet" href="style".$_SESSION['stylesheet'].".css" type="text/css">";
}

if ("'$_POST[default]'")
{
echo "<link rel="stylesheet" href="style1.css" type="text/css">";
$_SESSION['stylesheet'] = "1";
}
elseif ("'$_POST[black]'")
{

echo "<link rel="stylesheet" href="style2.css" type="text/css">";
$_SESSION['stylesheet'] = "2";
}
elseif ("'$_POST[purple]'")
{
echo "<link rel="stylesheet" href="style3.css" type="text/css">";
$_SESSION['stylesheet'] = "3";
}
elseif ("'$_POST[white]'")
{
echo "<link rel="stylesheet" href="style4.css" type="text/css">";
$_SESSION['stylesheet'] = "4";
}
else
{
echo "<link rel="stylesheet" href="style1.css" type="text/css">";
//rather than no styles, add the default
}


echo "</head>";

echo "<body>";


echo "<form action="index.php" method="post" >
<input type="radio" value="default" name="default">Default</option>
<input type="radio" value="black" name="black">Black</option>
<input type="radio" value="purple" name="purple">Purple</option>
<input type="radio" value="white" name="white">White</option>
<input type="submit" name="Change Style" />

</form>";



echo "<div class="nav"><a href="index.php">Home</a> | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>";


echo "</div>";


echo "</body></html>";

?>
[/code]
Copy linkTweet thisAlerts:
@knowjOct 24.2006 — Just a thought, what you have done looks alright, You may want to set a cookie or session when the user selects a style sheet, and you need to add a method to your form.

[code=php]
<?php

session_start();

echo "<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">";
echo "<head><title> {$title} </title>";


if(isset($_SESSION['stylesheet'])){
echo "<link rel="stylesheet" href="style".$_SESSION['stylesheet'].".css" type="text/css">";
}

if ("'$_POST[default]'")
{
echo "<link rel="stylesheet" href="style1.css" type="text/css">";
$_SESSION['stylesheet'] = "1";
}
elseif ("'$_POST[black]'")
{

echo "<link rel="stylesheet" href="style2.css" type="text/css">";
$_SESSION['stylesheet'] = "2";
}
elseif ("'$_POST[purple]'")
{
echo "<link rel="stylesheet" href="style3.css" type="text/css">";
$_SESSION['stylesheet'] = "3";
}
elseif ("'$_POST[white]'")
{
echo "<link rel="stylesheet" href="style4.css" type="text/css">";
$_SESSION['stylesheet'] = "4";
}
else
{
echo "<link rel="stylesheet" href="style1.css" type="text/css">";
//rather than no styles, add the default
}


echo "</head>";

echo "<body>";


echo "<form action="index.php" method="post" >
<input type="radio" value="default" name="default">Default</option>
<input type="radio" value="black" name="black">Black</option>
<input type="radio" value="purple" name="purple">Purple</option>
<input type="radio" value="white" name="white">White</option>
<input type="submit" name="Change Style" />

</form>";



echo "<div class="nav"><a href="index.php">Home</a> | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>";


echo "</div>";


echo "</body></html>";

?>
[/code]
[/QUOTE]



adding sessions only makes it work after transition to a page past the first post.

from the sounds what what he said and the looks of the code it wont work at all even from 1st post.

currently its posting all of the form variables as different posts which isn't needed only the 1 selected needs to be posted.

if the code was right the page would work on 1st click but loose the template after any transition/refresh without renewing the post data.

the names of all the radio boxes should be the same and the values be different.
Copy linkTweet thisAlerts:
@BleedauthorOct 26.2006 — knowj, you were correct. I couldn't get iit to work at all. I'll go back and add sessions once I get it to work without them. ? I'll test what you've corrected for me and get back to you.

All of your help is appreciated. Thanks,
Copy linkTweet thisAlerts:
@BleedauthorOct 27.2006 — Okay, this is what I have now based on the above corrections:

[code=php]
<?php

$title = "Template Page";

echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd" />';
echo '<head><title>' ,$title, '</title>';

switch ($_POST['style'])
{
case "default":
echo '<link rel="stylesheet" href="style1.css" type="text/css"/>';
break;
case "black":
echo '<link rel="stylesheet" href="style2.css" type="text/css"/>';
break;
case "purple":
echo '<link rel="stylesheet" href="style3.css" type="text/css"/>';
break;
case "white":
echo '<link rel="stylesheet" href="style4.css" type="text/css">';
break;
case 0:
echo '<link rel="stylesheet" href="style1.css" type="text/css"/>';
break;
}


echo '</head>';

echo '<form action="index.php" method="post">
<select name="Template">
<option type="radio" value="default" name="style">Default</option>
<option type="radio" value="black" name="style">Black</option>
<option type="radio" value="purple" name="style">Purple</option>
<option type="radio" value="white" name="style">White</option></select>
<input type="submit" name="Change Style" /></form>';

echo '<div class="nav"><a href="index.php">Home</a> | <a href="register.php">Register</a> | <a href="links.php">Links</a> | <a href="faq.php">FAQ</a></div>';

echo '</body></html>';

?>
[/code]


And here's 1 of 4 style sheets:

[code=php]

<style type="text/css">

#forms
{
position:absolute;
top:75px;left:200px;
align:right;
padding-bottom:5px;
}

#text
{
position:absolute;
top:75px;
left:125px;
align:left;
}

div.text2
{
padding-top:8px;
}

div.forms2
{
padding-bottom:6px;
}

#login
{
position:absolute;
top:100px;
left:75%;
}

div.nav
{
position:absolute;
top:75px;
left:10%;
right:10%;
width:80%;
background-color:#0099FF;
text-align:right;
}

.input
{
width:100px;
}

body
{
background-color:#333333;
}

</style>



[/code]


When I select and reload the page it just shows the content with a white background, not the bg color specified in the CSS.
Copy linkTweet thisAlerts:
@BleedauthorOct 30.2006 — I'm not quite sure why I can't get this to work, but if someone could point out what I'm doing incorrectly I'd greatly appreciate it.
×

Success!

Help @Bleed spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...