title: How to set a PHP session value as selected onclick on a drop down list
body: <p>I have the following code.</p>
  
  <pre><code>&lt;?php
       session_start();
  ?&gt;
   &lt;form action = "index.php" method = "post" name = "test"&gt;
      &lt;!--&lt;div id ="custom-select" style="width:200px;" onchange = "favsports()"&gt; --&gt;
         &lt;div class = "custom-select"&gt;
          &lt;div class = "select"&gt;
             &lt;select id ="custom-select" name = "custom-select" style="width:200px;" onchange = " this.form.submit(); myFunction();"&gt;
                  &lt;option value="Abteilung auswählen" &lt;?php if(isset($_POST['custom-select']) &amp;&amp; $_POST['custom-select'] == 'Abteilung auswählen' || $_SESSION['custom-select'] == 'Abteilung auswählen') echo 'selected="selected" '; ?&gt; &gt;Abteilung auswählen&lt;/option&gt;
                  &lt;option value="TL-311" &lt;?php if(isset($_POST['custom-select']) &amp;&amp; $_POST['custom-select'] == 'TL-311' || $_SESSION['custom-select'] == 'TL-311') echo 'selected="selected" '; ?&gt; &gt;TL-311&lt;/option&gt;
                  &lt;option value="TP-271" &lt;?php if(isset($_POST['custom-select']) &amp;&amp; $_POST['custom-select'] == 'TP-271' || $_SESSION['custom-select'] == 'TP-271') echo 'selected="selected" '; ?&gt; &gt;TP-271&lt;/option&gt;
                  &lt;option value="TP-310" &lt;?php if (isset($_POST['custom-select']) &amp;&amp; $_POST['custom-select'] == 'TP-310' || $_SESSION['custom-select'] =='TP-310') echo 'selected="selected" '; ?&gt; &gt;TP-310&lt;/option&gt;
          &lt;/select&gt;
         &lt;/div&gt;
        &lt;/div&gt;
   &lt;/form&gt;
  &lt;?php
  
       if (isset($_POST["custom-select"]))
       {
          $_SESSION['custom-select'] = $_POST["custom-select"];
          //print_r($_SESSION);
       }
  
  ?&gt;
  </code></pre>
  
  <p>I am using the same code in two pages because the selected value in the first page schould still be selected when the user go on the next page.
  I am also using the following Javascript code because the selected value schould be save and useon the Pages.</p>
  
  <pre><code>function myFunction()
  {
  
     var optione = '&lt;?php echo $_SESSION['custom-select']; ?&gt;';
       if(optione !== '')
       {
          document.getElementById("custom-select").value = optione;
       }
         document.getElementById("insert").value = optione;
         var mytext = optione;
       if (mytext == "Abteilung auswählen")
         { 
            alert("Wählen Sie bitte eine gültige Abteilung!");
            mytext = null;
            file = "TL-311";
          }
       else 
          {
            mytext = optione;
            window.file = mytext;
          }
       return window.file;
  }
  </code></pre>
  
  <p>The code is working but my problem is that: I have to select a value twice before setting the selected value and when I want to change the selected value, I must also select the other value twice before setting it as selected. can anyone tell me what I'm doing wrong? or What is actually wrong in my code? Please I am new in PHP and HTML programming.</p>