The code below will generate an HTML select tag and options using a text file having list of countries. If the file doesn't exist or contains only empty / white space lines then generate an input box instead of select tag. --------------------------------------------------------- generate_select_tag_and option_from_list_of_countries.php --------------------------------------------------------- ``` <?php $file = "countries.txt"; $file_handle = @fopen($file, "r"); $line = ""; $valid_line_found = false; $label_input_html = '<label style="position:absolute;right:70%;color:black;">** Select Country: </label>' . "\n"; $label_input_html = $label_input_html . '<input style="position:absolute;left:31%;" type="text" name="country" id="country" maxlength="40" size="40"><br><br>' . "\n"; $label_input_html = $label_input_html . "\n"; if ($file_handle == false) { echo $label_input_html; goto END; } while(!feof($file_handle)) { $line = fgets($file_handle); $line = trim($line); if (empty($line)) { continue; } else { $valid_line_found = true; break; } } // end of while loop if ($valid_line_found == false) { echo $label_input_html; fclose($file_handle); goto END; } echo '<label for="country" style="position:absolute;right:70%;color:black;">** Select Country: </label>' . "\n"; echo '<select name="country" id="country" size="1" style="position:absolute;left:31%;" maxlength="40" size="40">' . "\n"; echo "\t". '<option value=""></option>' . "\n"; echo "\t". '<option value="' . $line . '">' . $line . '</option>' . "\n"; while(!feof($file_handle)) { $line = fgets($file_handle); $line = trim($line); if (empty($line)) { continue; } echo "\t". '<option value="' . $line . '">' . $line . '</option>' . "\n"; } // end of while loop echo '</select><br><br>' . "\n"; echo "\n"; fclose($file_handle); END: ?> ``` The file having list of countries is below: ------------- countries.txt ------------- ``` Afghanistan Albania Algeria Andorra Angola Antigua and Barbuda Argentina Armenia Australia Austria Azerbaijan Bahamas Bahrain Bangladesh Barbados Belarus Belgium Belize Benin Bhutan Bolivia Bosnia and Herzegovina Botswana Brazil Brunei Bulgaria Burkina Faso Burundi Cambodia Cameroon Canada Cape Verde Central African Republic Chad Chile China Colombia Comoros Congo Costa Rica Croatia Cuba Cyprus Czech Republic DR Congo Denmark Djibouti Dominica Dominican Republic East Timor Ecuador Egypt El Salvador Equatorial Guinea Eritrea Estonia Eswatini Ethiopia Fiji Finland France Gabon Gambia Georgia Germany Ghana Greece Grenada Guatemala Guinea Guinea-Bissau Guyana Haiti Honduras Hungary Iceland India Indonesia Iran Iraq Ireland Israel Italy Ivory Coast Jamaica Japan Jordan Kazakhstan Kenya Kiribati Kuwait Kyrgyzstan Laos Latvia Lebanon Lesotho Liberia Libya Liechtenstein Lithuania Luxembourg Madagascar Malawi Malaysia Maldives Mali Malta Marshall Islands Mauritania Mauritius Mexico Micronesia Moldova Monaco Mongolia Montenegro Morocco Mozambique Myanmar Namibia Nauru Nepal Netherlands New Zealand Nicaragua Niger Nigeria North Korea North Macedonia Norway Oman Pakistan Palau Palestine Panama Papua New Guinea Paraguay Peru Philippines Poland Portugal Qatar Romania Russia Rwanda Saint Kitts and Nevis Saint Lucia Saint Vincent and the Grenadines Samoa San Marino Saudi Arabia Senegal Serbia Seychelles Sierra Leone Singapore Slovakia Slovenia Solomon Islands Somalia South Africa South Korea South Sudan Spain Sri Lanka Sudan Suriname Sweden Switzerland Syria São Tomé and Príncipe Tajikistan Tanzania Thailand Togo Tonga Trinidad and Tobago Tunisia Turkey Turkmenistan Tuvalu Uganda Ukraine United Arab Emirates United Kingdom United States Uruguay Uzbekistan Vanuatu Vatican City Venezuela Vietnam Yemen Zambia Zimbabwe ```