1

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>
2
  • Not sure if this is the issue, but doing selected="selected" doesn't work on Firefox. Commented Apr 26, 2020 at 12:48
  • Tested, it works with Firefox. Commented Apr 26, 2020 at 13:38

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.