我的问题是: 我有一个txt文件,其中包含城市名称,逐行:
21-citiesNames.txt:
Paris
London
Amsterdam
Brussel
Berlin
我有一个html文件(我不确定它是否假设是一个php文件而不是btw),在其中,我想要创建一个html表单,并且我希望以这种形式用户输入他的姓名,然后从下拉列表中选择一个城市。
我想逐行导入文本文件(从上面),作为选择列表选项。当然,保存用户的名称和城市选择,并将它们打印在不同的php文件中(如此 - "选择:(名称),(城市)。")。
现在,如果用户没有选择城市,那么该php文件将打印出来"已选择:(名称),您没有选择任何城市。"
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>
现在接收并打印用户输入的php文件:
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.".";
?>
还有一件事......我想打开一个新的txt文件,在其中我想要保存所有用户的过去输入,名称和城市,一个接一个,每次动态更改用户输入新的输入。
我目前的问题是我运行localhost / 21-scriptUseForm.html,页面是这样的:
输入您的姓名:[输入姓名的地方]
输入您的城市:[根本不选择选项。]
点击此处(提交按钮)
这就是它。我到处寻找答案,例如:
load text file list into <option> tags
php read file and create drop down menu
Creating a select dropdown from a .txt?
还有更多...请帮助,非常感谢。
答案 0 :(得分:0)
问题可能是您在标准HTML文件中使用PHP - 将21-scriptUseForm.html更改为21-scriptUseForm.php
虽然可以配置PHP解析的.html文件,但这通常不是默认行为。