A simple problem with a simple solution, I suppose. I have researced it but do not quite understand it.

Problem: I have a very nice script for a dropdown that displays country names and the associated flags for each country within a form. This works very well, but...

Examples of this code show all country/country codes embedded directly into the HTML page. While this works, it may be difficult to manage, it is too long and honestly, plain ugly.

I want to create a .txt file of the country names/country codes that lives on my server and can be called from within the HTML page. Clean.

I have read this is advised against (I do not understand why) - It is simple isolation of text to be called. How can that pose a problem or a risk?

I'm lost at this point as how to do this technically; I am assuming via Javascript/jQuery.

Thank you in advance for all help!


Edited by mattyd

3 Years
Discussion Span
Last Post by diafol

I don't see a problem with having a text file on your web server and then you retrieving that text using javascript/jQuery AJAX.

Can you elaborate regarding the risk you read about?


The risk. They did not state the risk. They just said that they read about it somewhere and added "LOL" - No details as why one should not implement this.

Edited by mattyd


As long as your text file is secured on the web server, I can't think of any reason.


In addition, you could use a PHP array, for example create fruits.php:


    return array(

        'red' => array(

        'green' => array(

        'blue' => array(


Then you can include it in your script as a configuration file:


    $fruits = include 'fruits.php';

    foreach($fruits['blue'] as $fruit)
        echo $fruit;

With this structure you can create a multidimensional array with all the information about each Country and use it as you like. Bye!

Edited by cereal

Votes + Comments
Thank you!

What I'm having trouble with is calling it from the server into the form.

I'm not exactly sure how to do this.



Here is some sample code for you to work with. The are generally two ways that you want to store data jSON and XML, at least those are the two i use when working with javascript. I will store my data for this example in an XML file. You will need to files for this sample to work.


<!DOCTYPE html>
     <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<form name="myForm" method="GET" action="">
   <select id="country-list">
      <option />
$(function () {
        type: "GET",
        url: "test.xml",
        dataType: "xml",
        success: function (xml) {
           $(xml).find('country').each(function (i) {
               var c = $(this).text();
               $('#country-list').append("<option>" + c + "</option>");


<?xml version="1.0" encoding="utf-8" ?>
     <country>United States</country>
Votes + Comments
Thank you!

Just remember that if you're using js for this all your code is visible and the locations of any files. For this I can't see a problem, however a server side solution may be easier - no need for Ajax.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.