/    Sign up×
Community /Pin to ProfileBookmark

GET Variable / Set Cookie / Get Cookie and use error

I’ve got a page which lists years since 1900 in a select box. When the user changes this, it reloads the page and places the selected value in the URL.

I’m then using PHP to set this as a cookie.

The following section of code gets the cookie IF it’s set otherwise using this current year.

Back down to my list, I’m trying to get the last selected item to show as selected.

The issue is it’s never showing the latest.

Right now, if I select 2019 the page reloads then the previous item is selected.

Change it to 2020 and 2019 shows as selected and is echoed above.

Change it to 1975 and 2020 is shown. If that makes sense.

Why is this?

Here is my code

https://jsfiddle.net/y4wmhevx/

I keep getting “You do not have permission to do that.” error so can’t post code into here.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 07.2022 — Not sure if this is the issue, but note that setcookie() does not set anything into $_COOKIE -- that only gets set when session_start() populates $_COOKIE from the cookies supplied by the incoming HTTP request. Maybe change the first if to:
[code=php]
if (isset($_GET['Year'])){
$_COOKIE['selectedYear'] = $_GET['Year'], time() + (86400 * 30);
setcookie('selectedYear', $_COOKIE['selectedYear'], '/');
}
[/code]
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...