title: Pass txt file lines to html file POST form select options, and retrieve option selected to a php file
body: <p>honestly I've been looking for an answer for my problem all over Stack Overflow for the last 2 days. Please don't downgrade my question...</p>
  
  <p>My problem is this: 
  I have a txt file that has names of cities in it, line by line:</p>
  
  <p><strong>21-citiesNames.txt:</strong></p>
  
  <pre><code>Paris
  London
  Amsterdam
  Brussel
  Berlin
  </code></pre>
  
  <p>I have an html file (which I'm not sure if it's suppose to be a php file instead btw), in it, I want to create an html form, and in that form I want the user to enter his name, and choose a city from a dropdown select list. </p>
  
  <p>I want to import the text file (from above), line by line of course, as the select list options. And of course save user's name and city selection and print them in a different php file (like so - "Selected: (name), (city)."). </p>
  
  <p>Now, if the user didn't choose a city, then that php file will print "Selected: (name), You didn't choose any city."</p>
  
  <p><strong>21-scriptUseForm.html:</strong></p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  
  &lt;body&gt;
  
      &lt;!--Form for choice name and city:--&gt;
      &lt;form method='POST' action='21-script.php'&gt;
          Enter your name:
          &lt;input type='text' name='userName'/&gt;
          &lt;br&gt;&lt;br&gt;
          Enter your city:
  
          &lt;select name='city'&gt;
              &lt;?
                  define("fileName", "21-citiesNames.txt");
                  global $cities;
                  global $options='';
                  $cityLines=file_get_contents(fileName);
                  $cities=explode("\n",$cityLines);
  
                  foreach($cities as $city){
                          $options.='&lt;option value="'.$city.'"&gt;'.$city.'&lt;/option&gt;';
                  }
                  echo $options;
              ?&gt;
              &lt;option value='city'&gt; &lt;?echo $options?&gt;&lt;/option&gt;
          &lt;/select&gt;
  
          &lt;br&gt;&lt;br&gt;
          &lt;input type=submit value="Click here"&gt;
      &lt;/form&gt;
  
  &lt;/body&gt; 
  &lt;/html&gt;
  </code></pre>
  
  <p>And now the php file that receives and prints the user's inputs:</p>
  
  <p><strong>21-script.php:</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  
  $_SESSION['userName'] = $_POST['userName'];
  
  if(!isset($_POST['city']))
      $cityChosenOrNot= "You didn't choose any city";
  else
  {
      $_SESSION['cityChosen'] = $_POST['city'];
      $cityChosenOrNot=$_POST['city'];
  }
  
  $name=$_POST['userName'];
  
  echo "Selected: ".$name.", ".$cityChosenOrNot.".";
  
  ?&gt;
  </code></pre>
  
  <p>There is one more thing... I want to open a new txt file and in it I want to save all of the user's past inputs, name and city, one after another, dynamically changing every time the user enters new inputs.</p>
  
  <p>My problem currently is that I run localhost/21-scriptUseForm.html and the page is like this:</p>
  
  <p>Enter your name: [place to enter name]</p>
  
  <p>Enter your city: [select with no options at all.]</p>
  
  <p>Click here (submit button)</p>
  
  <p>So that's it. I looked for an answer everywhere, for example:</p>
  
  <p><a href="https://stackoverflow.com/questions/3767076/load-text-file-list-into-option-tags">load text file list into &lt;option&gt; tags</a></p>
  
  <p><a href="https://stackoverflow.com/questions/16860647/php-read-file-and-create-drop-down-menu">php read file and create drop down menu</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2333023/creating-a-select-dropdown-from-a-txt">Creating a select dropdown from a .txt?</a></p>
  
  <p>And more... Please help, thank you very much.</p>