Php 从文本文件填充下拉列表时,值被设置为递增的数字,而不是内容
我设法找到了一种方法,从一个txt文件中填充一个“国家”下拉菜单,所有选项都在一行上 每个项的值都被赋予一个递增的数字,并通过fine传递 但是,我如何通过文本文件中的行内容,而不必为每个数字值创建一个巨大的开关来设置国家Php 从文本文件填充下拉列表时,值被设置为递增的数字,而不是内容,php,forms,Php,Forms,我设法找到了一种方法,从一个txt文件中填充一个“国家”下拉菜单,所有选项都在一行上 每个项的值都被赋予一个递增的数字,并通过fine传递 但是,我如何通过文本文件中的行内容,而不必为每个数字值创建一个巨大的开关来设置国家 <select class="country-select" name="country" tabindex = '9' > <?php $lines = file( 'country-list.txt' ); for ($i = 0; $i <
<select class="country-select" name="country" tabindex = '9' >
<?php
$lines = file( 'country-list.txt' );
for ($i = 0; $i < count($lines);$i++) {
echo '<option value=' . ($i + 1) . '>' . $lines[$i] . '</option>';
}
?>
</select>
$name = clearData($_POST["name"]);
$country = ($_POST["country"]);
$phone = ($_POST["phone"]);
值得一提的是,我收到的电子邮件回复显示了该值的数字。这是文本文件中的行号,对应于在下拉菜单中选择的正确国家/地区。因此,它正在选择并通过正确的国家/地区。尝试使用这些功能;它应该适合你的目的
// Get an array containing valid countries.
function GetCountries()
{
$lines = file('country-list.txt');
return $lines;
}
// Get the name of the country from the specified line number (its index in the array)
function GetCountryName($countryIndex)
{
$countries = GetCountries();
// It looks like your values for the <select> elements are not zero-based, so you might want to apply that modification here. Uncomment the following line if that is the case.
// $countryIndex = $countryIndex - 1;
$countryName = $countries[$countryIndex];
return $countryName;
}
//获取包含有效国家/地区的数组。
职能(国家)
{
$lines=文件('country-list.txt');
返回$line;
}
//从指定的行号(其在数组中的索引)获取国家的名称
函数GetCountryName($countryIndex)
{
$countries=GetCountries();
//看起来元素的值不是基于零的,因此您可能希望在此处应用该修改。
//$countryIndex=$countryIndex-1;
$countryName=$countries[$countryIndex];
返回$countryName;
}
你能发布一个“country list.txt”的示例吗?我不完全确定你在问什么;是否希望$country
的值作为所选国家的名称?如果是这样的话,是什么阻止你只做$country=$lines[$\u POST[“country”]代码>差不多,是的。我能想到的唯一简单方法是为每个行号创建一个长开关,并手动将其设置为国家名称。在看到您的回复之前,我编辑了我的上述评论并提出了建议。这行吗?不,它返回空值。如果我能让开关工作,我可能会坚持使用它。这非常有效,尽管在取消注释$countryName
行后,我需要将该值更改为-1
愚蠢的我,试图解决问题,并做了与需要做的相反的事情。为了正确起见,我会继续更新我的答案。
// Get an array containing valid countries.
function GetCountries()
{
$lines = file('country-list.txt');
return $lines;
}
// Get the name of the country from the specified line number (its index in the array)
function GetCountryName($countryIndex)
{
$countries = GetCountries();
// It looks like your values for the <select> elements are not zero-based, so you might want to apply that modification here. Uncomment the following line if that is the case.
// $countryIndex = $countryIndex - 1;
$countryName = $countries[$countryIndex];
return $countryName;
}