Jump to content

Php Sessions with html <select>


NuMan

Recommended Posts

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

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.

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.