Pass txt file lines to html file POST form select options, and retrieve option selected to a php file

1.4k Views Asked by At

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...

My problem is this: I have a txt file that has names of cities in it, line by line:

21-citiesNames.txt:

Paris
London
Amsterdam
Brussel
Berlin

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.

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).").

Now, if the user didn't choose a city, then that php file will print "Selected: (name), You didn't choose any city."

21-scriptUseForm.html:

<html>
<head></head>

<body>

    <!--Form for choice name and city:-->
    <form method='POST' action='21-script.php'>
        Enter your name:
        <input type='text' name='userName'/>
        <br><br>
        Enter your city:

        <select name='city'>
            <?
                define("fileName", "21-citiesNames.txt");
                global $cities;
                global $options='';
                $cityLines=file_get_contents(fileName);
                $cities=explode("\n",$cityLines);

                foreach($cities as $city){
                        $options.='<option value="'.$city.'">'.$city.'</option>';
                }
                echo $options;
            ?>
            <option value='city'> <?echo $options?></option>
        </select>

        <br><br>
        <input type=submit value="Click here">
    </form>

</body> 
</html>

And now the php file that receives and prints the user's inputs:

21-script.php:

<?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.".";

?>

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.

My problem currently is that I run localhost/21-scriptUseForm.html and the page is like this:

Enter your name: [place to enter name]

Enter your city: [select with no options at all.]

Click here (submit button)

So that's it. I looked for an answer everywhere, for example:

load text file list into <option> tags

php read file and create drop down menu

Creating a select dropdown from a .txt?

And more... Please help, thank you very much.

1

There are 1 best solutions below

2
On

The problem is probably that you are using PHP in a standard HTML file - change 21-scriptUseForm.html to 21-scriptUseForm.php

Whilst it is possible to configure .html files to be parsed by PHP this is not usually default behaviour.