dongshi1869 2014-08-13 07:16
浏览 35
已采纳

从文本文件填充下拉列表,值被设置为递增数而不是内容

I've managed to pull together a way to populate a 'Country' dropdown menu from a txt file, with all of the options on a line each.

The value of each item is given an incrementing number, and passes this through fine.

But how can I pass through the contents of the line from the text file, without having to create a massive switch for each of the number values, to set the country?

<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"]);

A small portion of my country file:

Your Country
-
Afghanistan
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antarctica
Antigua/Barbuda
Argentina
Armenia
Aruba
Australia

It's worth saying, that the email response I receive, shows the number of the value. Which is the line number in the text file, and that corresponds to the correct country chosen in the dropdown menu. So it is selecting and passing through the correct country.

展开全部

  • 写回答

1条回答 默认 最新

  • dongpin6941 2014-08-13 07:47
    关注

    Try using these functions; it should work for your purposes.

    // 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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?