我已经设法从一个txt文件中填充一个'Country'下拉菜单,所有的选项都在一行上。
给每一项的值赋一个递增的数字,并将其传递给fine。
但是,我如何从文本文件中传递行内容,而不必为每个数字值创建大量的开关来设置国家?
<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"]);
my country文件的一小部分:
Your Country
-
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua/Barbuda
Argentina
Armenia
Aruba
Australia
值得一提的是,我收到的电子邮件回复显示了值的数字。这是文本文件中的行号,它对应于下拉菜单中选择的正确国家。所以它选择并通过了正确的国家。
尝试使用这些函数;它应该能满足你的需要。
// 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;
}