suess0r Posted December 24, 2006 Share Posted December 24, 2006 hello,OK i'm POSTing over a city which the user chooses which set's the cookie for their city. When the cookie is set it says "Your city is ___" if it's not set it has a link to set your city, the problem is that after I choose my city and it takes me to the next page it doesn't echo the cookie until after I refresh the page. It's like if I echo'd before I set the cookie but that's not the case.. here's the code, can someone help :7[quote]<?php$time = time(); $city = $_REQUEST['city_pick'];if($city != ""){ setcookie("cookie_city", $city, time()+3600);}?>--header and body-- <?php if (!isset($_COOKIE['cookie_city'])) { echo '<a href="/mcity.php">Please select a city</a>'; } else { $cookie_parts = explode("&", $_COOKIE['cookie_city']); echo "Your cookie is set to {$cookie_parts[0]}!"; } ?>[/quote] Link to comment https://forums.phpfreaks.com/topic/31781-cookie-not-echoing-until-after-i-refresh-page/ Share on other sites More sharing options...
Psycho Posted December 24, 2006 Share Posted December 24, 2006 From the manual" "Once the cookies have been set, they can be accessed on [b]the next page load[/b]"Chjange your code to this:[code]<?php$time = time(); if (isset($_REQUEST['city_pick']) && $_REQUEST['city_pick']!="") { $city = $_REQUEST['city_pick']; setcookie('city_pick', $city, time()+3600);} elseif (isset($_COOKIE['city_pick'])) { $city = $_COOKIE['city_pick'];}if (!isset($city)) { echo '<a href="/mcity.php">Please select a city[/url]';} else { echo "Your cookie is set to {$city}!";}?>[/code] Link to comment https://forums.phpfreaks.com/topic/31781-cookie-not-echoing-until-after-i-refresh-page/#findComment-147404 Share on other sites More sharing options...
suess0r Posted December 26, 2006 Author Share Posted December 26, 2006 OK, so it's echo'ing the cookie fine, but here's how i laid it out and when i go to 'change cities' it is still set to the original cookie and it's not resetting the cookie to the new one.. anyone help?[quote]<?php$time = time(); if (isset($_REQUEST['city_pick']) && $_REQUEST['city_pick']!="") { $city = $_REQUEST['city_pick']; setcookie('city_pick', $city, time()+3600);} elseif (isset($_COOKIE['city_pick'])) { $city = $_COOKIE['city_pick'];} if (!isset($city)) { echo '<a href="/mcity.php">Please select a city</a>'; } else { echo "Your city is: {$city}!<br>"; echo '<a href="/mcity.php">Change cities</a>'; }?>[/quote] Link to comment https://forums.phpfreaks.com/topic/31781-cookie-not-echoing-until-after-i-refresh-page/#findComment-148029 Share on other sites More sharing options...
Psycho Posted December 26, 2006 Share Posted December 26, 2006 How are you "changing" city? I think we need to see the entire page - more specifically the form itself. Link to comment https://forums.phpfreaks.com/topic/31781-cookie-not-echoing-until-after-i-refresh-page/#findComment-148071 Share on other sites More sharing options...
suess0r Posted December 26, 2006 Author Share Posted December 26, 2006 I'm "changing" cities the same way i'm setting the city by giving a link to mcity.php, on that page i have this form<form action="mcooktest.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> <div id="choose_article"> <div align="left"><span class="style3"><br> Choose your city: </span> <select name="city_pick" class="field select medium" style="width: 160px;"> <option value=""></option> <?phpdo { ?> <option value="<?php echo $row_city['City']?>"><?php echo $row_city['City']?></option> <?php} while ($row_city = mysql_fetch_assoc($city)); $rows = mysql_num_rows($city); if($rows > 0) { mysql_data_seek($city, 0); $row_city = mysql_fetch_assoc($city); }?> </select> </div> </div> <br> <div id="choose_article"> <div align="left"></div> </div> <div id="choose_article"> <div id="choose_article"><br /> <input name="submit" type="submit" class="style7" value="Set my city" /> </div><br /> <div align="center"><span class="style6"></span><a href="#"></a></div> </div> </form>which basically populates the dropdown with a bunch of cities from a table. it then POST's to mcooktest.php the variable 'city_pick'. which the above code is what sets the cookie. It still only lets me set the cookie and it works great the first time, but when i try to set a different city after it takes me to mcity.php and i do the same process and when i submit it again and go to mcooktest.php it still says the first city i chose.let me know if that helps... Link to comment https://forums.phpfreaks.com/topic/31781-cookie-not-echoing-until-after-i-refresh-page/#findComment-148085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.