NuMan Posted January 7, 2009 Share Posted January 7, 2009 Hello, I am trying to make a style.css chooser for my site and i am having a bit of a problem getting the sessions. Here's what i have: form: <form method="post" action=""> <select name="styles"> <option value="green">green</option> <option value="red">red</option> <option value="blue">bime</option> </select><br> <input type="submit" value="go"> </form> now the action: <?php session_start(); $style = $_POST['styles']; $_SESSION['style'] = $style; ?> and finally to check for the sessions: <?php if($_SESSION['style'] === green){ $style = "styles/green/style.css"; } elseif($_SESSION['style'] === blue) { $style = "styles/blue/style.css"; } elseif($_SESSION['style'] === red) { $style = "styles/red/style.css"; } elseif($_SESSION['style'] === FALSE) { $style = "styles/green/style.css"; } echo $style; This is not working, help me please Link to comment https://forums.phpfreaks.com/topic/139813-php-sessions-with-html/ Share on other sites More sharing options...
genericnumber1 Posted January 7, 2009 Share Posted January 7, 2009 change your last one to <?php // Enforce the default style if(!isset($_SESSION['style']) || !in_array($_SESSION['style'], array('red', 'blue', 'green')) { $_SESSION['style'] = 'green'; } $style = "styles/{$_SESSION['style']}/style.css"; ?> and make sure (if it's not on the same page as the middle code section) to add session_start() to the top of the page. PS. You can remove the magic constants if you want, if you don't know what a magic constant is, then there's no need to even worry about it until you want to focus on good coding practices. Link to comment https://forums.phpfreaks.com/topic/139813-php-sessions-with-html/#findComment-731451 Share on other sites More sharing options...
l_kris06 Posted January 7, 2009 Share Posted January 7, 2009 Fixed: <?php session_start(); $style = $_POST['styles']; $_SESSION['style'] = $style; ?> <?php if($_SESSION['style'] == "green"){ $styles = "styles/green/style.css"; }elseif($_SESSION['style'] == "red"){ $styles = "styles/red/style.css"; }elseif($_SESSION['style'] == "blue"){ $styles = "styles/blue/style.css"; }else{} if(!isset($_SESSION['style'])){ $styles = "styles/green/style.css"; } ?> <form method="post" action=""> <select name="styles" > <option value="green" <?php if($_SESSION['style'] == "green"){?> selected='selected' <?php }?> >green</option> <option value="red" <?php if($_SESSION['style'] == "red"){?> selected='selected' <?php }?> >red</option> <option value="blue" <?php if($_SESSION['style'] == "blue"){?> selected='selected' <?php }?> >bime</option> <option value="" <?php if(!isset($_SESSION['style'])){ ?> selected='selected' <?php } ?> >Select style</option> </select><br> <input type="submit" value="go"> </form> <?php echo $styles; ?> Rgds, Kris Link to comment https://forums.phpfreaks.com/topic/139813-php-sessions-with-html/#findComment-731456 Share on other sites More sharing options...
NuMan Posted January 7, 2009 Author Share Posted January 7, 2009 Fixed: <?php session_start(); $style = $_POST['styles']; $_SESSION['style'] = $style; ?> <?php if($_SESSION['style'] == "green"){ $styles = "styles/green/style.css"; }elseif($_SESSION['style'] == "red"){ $styles = "styles/red/style.css"; }elseif($_SESSION['style'] == "blue"){ $styles = "styles/blue/style.css"; }else{} if(!isset($_SESSION['style'])){ $styles = "styles/green/style.css"; } ?> <form method="post" action=""> <select name="styles" > <option value="green" <?php if($_SESSION['style'] == "green"){?> selected='selected' <?php }?> >green</option> <option value="red" <?php if($_SESSION['style'] == "red"){?> selected='selected' <?php }?> >red</option> <option value="blue" <?php if($_SESSION['style'] == "blue"){?> selected='selected' <?php }?> >bime</option> <option value="" <?php if(!isset($_SESSION['style'])){ ?> selected='selected' <?php } ?> >Select style</option> </select><br> <input type="submit" value="go"> </form> <?php echo $styles; ?> Rgds, Kris Worked, Thanks a lot! Link to comment https://forums.phpfreaks.com/topic/139813-php-sessions-with-html/#findComment-731558 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.