title: How to use a select form to change language with PHP $_GET and $_POST?
body: <p>I have to apologize if this answer has already been answered before, I've looked for it and found something partially useful but nothing that answered my needs.</p>
  
  <p>I'm new to PHP and I want to make a simple website with multilingual support. Translations are provided by specific arrays according to the page they are in. I will use a <code>foreach</code> loop to get the translation. User is allowed to change the default language with a <code>select</code> html tag. I am partially able to achieve this goal in this way:</p>
  
  <p>Firstly, with a simple function I look for the browser language and set it as language fall-back:</p>
  
  <pre><code>function browser_lang() {
    $rawLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  
    if($rawLang == 'it') {
      $browserLang = 'it';
    } else {
      $browserLang = 'en';
    }
  
    return $browserLang;
  }
  </code></pre>
  
  <p>In the <code>index.php</code> file I set the fall-back language in this way:</p>
  
  <pre><code>&lt;?php
      // Include the browser_lang() and other functions
  
      if(isset($_POST['set_language'])) {
          $lang = strip_bad_chars($_GET['lang']);
      } else {
          $lang = browser_lang();
      }
  ?&gt;
  
  &lt;html lang="&lt;?php echo $lang; ?&gt;"&gt;
  </code></pre>
  
  <p>Later in the html I added a form with some select tag that allows to select languages:</p>
  
  <pre><code>&lt;form action="" method="post"&gt;
    &lt;select name="set_language" id="custom-lang"&gt;
      &lt;option value="it"&gt;Italiano&lt;/option&gt;
      &lt;option value="en"&gt;English&lt;/option&gt;
    &lt;/select&gt;
    &lt;input type="submit" name="input_language" value="Set Language"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>When I load the page for the first time, the script is able to retrieve the browser language, but when I select a custom language, it is not able to change it.</p>
  
  <p>How can I add <code>?lang=en</code> or <code>?lang=it</code> at the end of the url in order to use <code>$_GET['lang']</code> and loop through translations with the <code>$lang</code> variable set with that select form?</p>
  
  <p>The entire <code>index.php</code>, included the external resources, is:</p>
  
  <pre><code>&lt;?php
      function browser_lang() {
          $rawLang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
  
          if($rawLang == 'it') {
              $browserLang = 'it';
          } else {
              $browserLang = 'en';
          }
  
          return $browserLang;
      }
  
      function strip_bad_chars( $input ) {
          $output = preg_replace( "/[^a-zA-Z0-9_-]/", "",$input);
          return $output;
      }
  
      if(isset($_POST['set_language'])) {
          $lang = strip_bad_chars($_GET['lang']);
      } else {
          $lang = browser_lang();
      }
  ?&gt;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="&lt;?php echo $lang; ?&gt;"&gt;
  &lt;head&gt;
      &lt;title&gt;HOME&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
      &lt;form action="" method="post"&gt;
          &lt;select name="set_language" id="custom-lang"&gt;
              &lt;option value="it"&gt;Italiano&lt;/option&gt;
              &lt;option value="en"&gt;English&lt;/option&gt;
          &lt;/select&gt;
          &lt;input type="submit" name="input_language" value="Set Language"&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>