Jump to content

Cookie not echoing until after I refresh page!??!


suess0r

Recommended Posts

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]
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]
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]
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>
      <?php
do { 
?>
      <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...

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.