Reputation: 51
Goal: When onChange script loads new URL, selected value has to change too for displaying new selected value.
Problem: Selected value does not update after onChange action but will do so after refresh. It will set the cookie "lng" to correct value as it should.
When user selects new language I set it to user cookie file under "lng". With PHP I get the value but it does not update the selected value.
Here is my code:
<select onChange="if (this.value) window.location.href=this.value" class="form-control">
<option <?php if($_COOKIE['lng'] === "EN") { echo "selected='seleceted'"; } ?> value="/">English</a></option>
<option <?php if($_COOKIE['lng'] === "FI") { echo "selected='seleceted'"; } ?> value="/fi/">Suomi</option>
<option <?php if($_COOKIE['lng'] === "ET") { echo "selected='seleceted'"; } ?> value="/et/">Eesti</option>
<option <?php if($_COOKIE['lng'] === "SV") { echo "selected='seleceted'"; } ?> value="/sv/">Svenska</option>
<option <?php if($_COOKIE['lng'] === "FR") { echo "selected='seleceted'"; } ?> value="/fr/">Français</option>
<option <?php if($_COOKIE['lng'] === "IT") { echo "selected='seleceted'"; } ?> value="/it/">Italiano</option>
</select>
Upvotes: 1
Views: 135
Reputation: 3710
Taken from this answer - set_cookie
will set the browser cookies when the response is sent from the server. $_COOKIE
only contains cookies sent from the browser to the server in the request. This requires that there be a full round-trip from the server to the client and back to the server before something you put in with set_cookie
will show up in $_COOKIE
.
This is why it works on page refresh and not initially.
Upvotes: 1