Reputation: 137
I was just messing with PHP trying to get my form to change color when you select a color. for this im using cookies.. only when I press the button at first the background doesnt change but when you press it again it does. why does it doe this?
here's my code (sorry for using html in php):
<?php
$red = "";
$blue = "";
$green = "";
$gold = "";
$silver = "";
$purple = "";
$hour = time() + 3600;
if (isset($_POST['order'])) {
$color = $_POST['order'];
$$color = "selected";
setcookie("Free_cookies", $color, $hour);
}
else{
$color = "red";
$red = "selected";
}
if(isset($_COOKIE['Free_cookies'])){
$color = $_COOKIE['Free_cookies'];
$$color = "selected";
}
?>
<form method='post'<?php echo "STYLE='background-color:".$color.";'";?> ><p id='txtorder' >color: </p>
<select name='order' id='order'>
<option value="red"<?php echo $red; ?> >red</option>
<option value="blue"<?php echo $blue; ?> >blue</option>
<option value="green"<?php echo $green; ?> >green</option>
<option value="gold"<?php echo $gold; ?> >gold</option>
<option value="silver"<?php echo $silver; ?> >silver</option>
<option value="purple"<?php echo $purple; ?> >purple</option>
</select>
<input type='submit' value='sort'/>
</form>
Upvotes: 1
Views: 2596
Reputation: 7315
The problem is you're trying to read a cookie that you've only just set using setcookie()
.
The cookie won't be available to read until next page load (cookies are stored locally on the client not the server, so PHP won't read it til it's been sent to the client and back again).
To fix, only do your cookie read if there's no $_POST
value, i.e. give the newly POSTed value priority:
<?php
$red = "";
$blue = "";
$green = "";
$gold = "";
$silver = "";
$purple = "";
$hour = time() + 3600;
// first check for a new value, and use it as well as saving it for next time
if (isset($_POST['order']))
{
$color = $_POST['order'];
$$color = " selected";
setcookie("Free_cookies", $color, $hour);
}
// if there's no new value, THEN check for a previous value in a cookie
else if(isset($_COOKIE['Free_cookies']))
{
$color = $_COOKIE['Free_cookies'];
$$color = " selected";
}
// otherwise default to red
else
{
$color = "red";
$red = " selected";
}
?>
<form method='post' <?php echo "STYLE='background-color:".$color.";'";?> ><p id='txtorder' >color: </p>
<select name='order' id='order'>
<option value="red" <?php echo $red; ?> >red</option>
<option value="blue" <?php echo $blue; ?> >blue</option>
<option value="green" <?php echo $green; ?> >green</option>
<option value="gold" <?php echo $gold; ?> >gold</option>
<option value="silver" <?php echo $silver; ?> >silver</option>
<option value="purple" <?php echo $purple; ?> >purple</option>
</select>
<input type='submit' value='sort'/>
</form>
Upvotes: 2
Reputation: 16989
It's because the cookie is remembering the last color being saved and you're overwriting the color you just submitted. Only after the second submit will the desired color show up. You should do something like this:
if (isset($_POST['order'])) {
$color = $_POST['order'];
$$color = "selected";
setcookie("Free_cookies", $color, $hour);
} else {
if (isset($_COOKIE['Free_cookies'])) {
$color = $_COOKIE['Free_cookies'];
$$color = "selected";
} else {
$color = "red";
$red = "selected";
}
}
In this case, it will always take the posted color you submit. If you land on the page and there's a cookie, it'll then take that. If the form is not submitted and there is no cookie, it'll use red as default.
Upvotes: 1