0

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!

Matthew

Edited by mattyd

4
Contributors
7
Replies
27
Views
3 Years
Discussion Span
Last Post by diafol
1

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?

0

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

1

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

2

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

<?php

    return array(

        'red' => array(
            'Cherries',
            'Cranberries',
            'Pomegranate',
        ),

        'green' => array(
            'Avocados',
            'Cucumbers',
            'Honeydew',
        ),

        'blue' => array(
            'Blueberries',
            'Fig',
            'Plums'
        )

    );

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

<?php

    $fruits = include 'fruits.php';
    print_r($fruits['red']);

    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!
0

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

I'm not exactly sure how to do this.

Thanks,
Matthew

2

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.

test.htm

<!DOCTYPE html>
<html>
<head>
     <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<body>
<form name="myForm" method="GET" action="">
   <select id="country-list">
      <option />
   </select>
</form>
<script>
$(function () {
   $.ajax({
        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>");
           });
        }
   });
});
</script>
</body>
</html>

test.xml

<?xml version="1.0" encoding="utf-8" ?>
<DropDown>
     <country>Australia</country>
     <country>Brazil</country>
     <country>Canada</country>
     <country>...</country>
     <country>...</country>
     <country>United States</country>
</DropDown>
Votes + Comments
Thank you!
1

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.