Help - Search - Members - Calendar
Full Version: Session from Form
Invision Power Services > Community Forums > Community Web Design and Coding
Michael Boutros
I am trying to create a template changer for my site. The layout stays the same but the CSS changes the colors. I have it working and everything, but I need to use sessions to remember the CSS sheet chosen so that the person does not have to choose the template everytime they go to a new page. This is the code I am using:

CODE
<?php

    session_start();
    
    // Time to choose the layout! Since it is the same layout with different style sheets, just put the users choice into a session,
    // then add a default incase the user hasn't chosen anything.
    
    if (isset($_GET["css_form"])){
        $HTTP_SESSION_VARS["css"] = $_GET["css_form"];
        $css = $_GET["css_form"];                
    } else {
        $css = "orange";
    }
    
    echo "<link rel='stylesheet' type='text/css' href='http://www.ephp.uni.cc/$css/style.css' title='Stylesheet'/>";
    
    
?>


And the form:
CODE
<form name="css_chooser" type="get">
            <select name="css_form">
            <option value="orange" selected> Orange
            <option value="green"> Green
            <option value="gray"> Gray
            <option value="pink"> Pink
            <option value="purple"> Purple
            <option value="red"> Red
            </select>
            <input type="submit" value="Go!">
            </form>


What should I do, becuase it isn't working.



And btw, where did you CODE button go?
Starnox
CODE
<?php

session_start();

// Time to choose the layout! Since it is the same layout with different style sheets, just put the users choice into a session,
// then add a default incase the user hasn't chosen anything.

if (isset($_POST['css_form'])){
$_SESSION['css'] = $_POST['css_form'];
} else {
$_SESSION['css'] = "orange";
}

echo '<link rel="stylesheet" type="text/css" href="http://www.ephp.uni.cc/'.$_SESSION['css'].'/style.css" title="Stylesheet" />';

?>


CODE
<form name="css_chooser" type="post">
<select name="css_form">
<option value="orange" selected> Orange
<option value="green"> Green
<option value="gray"> Gray
<option value="pink"> Pink
<option value="purple"> Purple
<option value="red"> Red
</select>
<input type="submit" value="Go!">
</form>


biggrin.gif
Michael Boutros
Now when I change it, it doesn't do anything. And also, it still has the get stuff at the top. Try it: http://www.ephp.uni.cc
Starnox
QUOTE(Jervous @ Jul 6 2005, 06:28 PM) *
Now when I change it, it doesn't do anything. And also, it still has the get stuff at the top. Try it: http://www.ephp.uni.cc


CODE
<form name="css_chooser" method="post" action="?">
<select name="css_form">
<option value="orange" selected> Orange
<option value="green"> Green
<option value="gray"> Gray
<option value="pink"> Pink
<option value="purple"> Purple
<option value="red"> Red
</select>
<input type="submit" value="Go!">
</form>
Michael Boutros
It works, but when I select a colour from the index page then go to the test page by clicking the link it doesn't change and the BG is orange. Try it out at ephp.uni.cc.
Kenny Pollock
I personally would use cookies for this.

http://www.alistapart.com/articles/phpswitch/
Michael Boutros
I wanted to but I know even less about Cookies and PHP than Sessions and PHP.
Starnox
FYI sessions use cookies tongue.gif

Make sure you add session_start(); at the top of every page, otherwise it will not read $_SESSION['css'];

original.gif
Kenny Pollock
Oh, I've never used sessions, heh- I've always used set cookie and if($_COOKIE['']) etc.
Starnox
Jervous if you want to send the files to me I will get it working and send them back?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.