RockingPanda
RockingPanda

Reputation: 9

How to make language drop down selected also stay on next page?

I am using this code to change hyperlink of a button and now I want that if the person selects a language for example French so it will be automatically selected when the next page opens. If I move from page1.php to page2.php. The language on the next page should be automatically selected to French. Can anybody guide me how this can be done?

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>

</head>

<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="custom.js">
</script>




  <select name="" id="lang">
    <option value="English">English</option>
    <option value="French">French</option>
    <option value="Spanish">Spanish</option>
  </select>

  <select name="" id="currency">
    <option value="USD">USD</option>
    <option value="EUR">EUR</option>
    <option value="MXN">MXN</option>
  </select>

  <a href="" id="theButton">Click</a>
  
    <a href="test2.php">Click</a>

</body>

</html>

$(document).ready(function(){
var saveclass = null;

function onChangeHandler() {
  const lang = document.querySelector('#lang').value;
  const currency = document.querySelector('#currency').value;
  var strLink = "https://example.com/index.php?lang="+lang+"&currency="+currency;
  document.querySelector('#theButton').setAttribute('href', strLink);

}

onChangeHandler();

document.querySelector('#lang').addEventListener('change', onChangeHandler);
document.querySelector('#currency').addEventListener('change', onChangeHandler);

}); 

Upvotes: 0

Views: 843

Answers (2)

Nelson Rakson
Nelson Rakson

Reputation: 566

Latest Answer

index.php

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script type="text/javascript" src="custom.js"></script>
  <script type="text/javascript" src="cookie_script.js"></script>
</head>

<body>

  <select name="" id="lang">
    <option <?php if(@$_COOKIE['lang']=="English"){ echo "selected"; }?> value="English">English</option>
    <option <?php if(@$_COOKIE['lang']=="French"){ echo "selected"; }?> value="French">French</option>
    <option <?php if(@$_COOKIE['lang']=="Spanish"){ echo "selected"; }?> value="Spanish">Spanish</option>
  </select>

  <select name="" id="currency">
    <option <?php if(@$_COOKIE['currency']=="USD"){ echo "selected"; }?> value="USD">USD</option>
    <option <?php if(@$_COOKIE['currency']=="EUR"){ echo "selected"; }?> value="EUR">EUR</option>
    <option <?php if(@$_COOKIE['currency']=="MXN"){ echo "selected"; }?> value="MXN">MXN</option>
  </select>

  <a href="test2.php">Click</a>

</body>

</html>

cookie_script.js

$(document).ready(function(){
  $('#lang').on('change', function(){ document.cookie = "lang="+$(this).val()+"; path=/"; });
  $('#currency').on('change', function(){ document.cookie = "currency="+$(this).val()+"; path=/"; });
});

Always include

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script type="text/javascript" src="cookie_script.js"></script>

...on every page

Upvotes: 1

ecrb
ecrb

Reputation: 710

On the next page, you need to read the URL and update your currency and language dropdowns with the values you got.

According to this post: How can I get query string values in JavaScript? you should use URLSearchParams.

Step 1 - Get the chosen language and currency values from the URL.

const urlParams = new URLSearchParams(window.location.search);
const language = urlParams.get('lang');
const currency = urlParams.get('currency');

Step 2 - Use the values to update the dropdown:

$('#lang').val(language);
$('#currency').val(currency);

If you don't want to keep doing this in every page, you can either save these settings in the client's machine (localStorage) or in a server.

Upvotes: 0

Related Questions