Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
need to get data from a text file
Old 03-10-2008, 05:02 PM need to get data from a text file
angele803's Avatar
Perfectly Imperfect

Posts: 1,774
Name: Stephanie
Location: Oklahoma
Trades: 2
First off, I admit right now that I know nothing about javascript and apologize for my stupidity.

I have several text files (named like the states ie. Oklahoma.txt, Texas.txt, etc.). Each text file has partial html code for several list items (a a list of several cities in that state). I need to take the html list items from those text files and put them into a div depending on which state a user selects.

My question is, is it possible for me to get the html code from one of these text file and plug that data into a variable?

If so, shouldn't I be able to display the results by using something like: document.getElementById('cities_list').innerHTML=c ities;
??
__________________

Please login or register to view this content. Registration is FREE
angele803 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-10-2008, 08:45 PM Re: need to get data from a text file
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
there are several approaches.
The first using whatever your server supports for an SSI (server side include) or equivalent.
The other, is to use the so called AJAX technique to load the correct file as needed (on demand).

Whichever you use to get the data, you will also have to do a little DOM manipulation to display the data.
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 03-11-2008, 12:41 PM Re: need to get data from a text file
angele803's Avatar
Perfectly Imperfect

Posts: 1,774
Name: Stephanie
Location: Oklahoma
Trades: 2
Thanks. I am using coldfusion. I found a way to read the text file and assign the contents of that text file into a coldfusion variable. I then can pass that variable to the javascript. This looks like it is working.

So I am passing a state name through as a variable from a link. Then I have to take the value of the state name and compare it with the value of the coldfusion variable I made for the text file. This works, but it is going to be one really really long if statement. Is there a better way to do this?

Example:

ColdFusion to Read a File (oklahoma and texas):
Code:
        <cflock name="text_file_cities" type="readonly" timeout="30">
        	<cffile action="read"
            	file="c:/filelocation/OK.txt"
                variable="Oklahoma"
                charset="utf-8">
        </cflock>

        <cflock name="text_file_cities" type="readonly" timeout="30">
        	<cffile action="read"
            	file="c:/filelocation/TX.txt"
                variable="Texas"
                charset="utf-8">
        </cflock>
ColdFusion variable to Javascript variable:
Code:
<cfoutput>
     var #toScript(Oklahoma, "Oklahoma")#;
</cfoutput>
Javascript code snippet:
Code:
function showdiv(state) { 
document.getElementById('cities_popup_container').style.visibility = 'visible'; 
document.getElementById('state_name').innerHTML=state;
		
if(state =='Oklahoma')
     document.getElementById('cities_list').innerHTML=Oklahoma          
 if(state =='Texas')
     document.getElementById('cities_list').innerHTML=Texas;
and so on.....
and so on.....
}
Link to page with javascript:
Code:
javascript:showdiv('Oklahoma')

javascript:showdiv('Texas')

And so on for most of the 50 states
Are the If Statements the best way to handle this?

Thanks!!
Stephanie
__________________

Please login or register to view this content. Registration is FREE

Last edited by angele803; 03-11-2008 at 12:44 PM..
angele803 is offline
Reply With Quote
View Public Profile
 
Old 03-11-2008, 12:51 PM Re: need to get data from a text file
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
Hi Stephanie, to exploit that the states and variables are the same, try changing your function to this:
Code:
function showdiv(state) { 
document.getElementById('cities_popup_container').style.visibility = 'visible'; 
document.getElementById('state_name').innerHTML=state;
document.getElementById('cities_list').innerHTML=eval(state);
}
Then you can eliminate the ifs altogether.

Hope that helps,
Paul
__________________

Please login or register to view this content. Registration is FREE


Last edited by willcode4beer; 03-11-2008 at 12:54 PM..
willcode4beer is offline
Reply With Quote
View Public Profile
 
Old 03-11-2008, 01:04 PM Re: need to get data from a text file
angele803's Avatar
Perfectly Imperfect

Posts: 1,774
Name: Stephanie
Location: Oklahoma
Trades: 2
Jackpot!!!

This is what I was hoping I could do. I was trying to google something like "how to set a variable to the value of a different variable" and was getting totally lost.

Thanks so much. It looks like this is working now!

-Stephanie
__________________

Please login or register to view this content. Registration is FREE
angele803 is offline
Reply With Quote
View Public Profile
 
Old 03-11-2008, 01:25 PM Re: need to get data from a text file
willcode4beer's Avatar
Super Moderator

Posts: 1,533
Name: Paul Davis
Location: San Francisco
Trades: 1
no problem :-)
__________________

Please login or register to view this content. Registration is FREE

willcode4beer is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to need to get data from a text file
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.46055 seconds with 12 queries