PDA

View Full Version : loading a .txt into a texarea


Mars
04-29-2003, 10:50 PM
how can i allow users to load a .txt file into a textarea using a browse... button.....thx

Mars
04-30-2003, 01:10 AM
ok here is what i got



<form name="idlist">

<select size="7" id="select1" name="parentid" multiple="false" selected="true">

<option value="1">Option 1</option>

</select>

</form>




i want to load a text file into that box and be able to copy and delete options from it....any help would be very much appreciated....thx

Spudhead
04-30-2003, 09:51 AM
ummm, ok.

That's not a textarea, that's a select box. What, exactly, do you want to happen? Users choose a text file from the select box to be loaded into a (seperate) textarea? The options in the select box are loaded from a text file? Something entirely different?

Whatever you want to do, I think you're going to need some sort of server-side language. ASP, PHP, JSP. Do you know any? Post a better explaination of what you're trying to achieve and we'll see what we can do - but be warned, this could get a lot more complicated than a HTML select box...

Mars
04-30-2003, 07:19 PM
sorry i didnt make myself clear....i want it to load a textfile from the user's c omputer

kwhubby
04-30-2003, 07:27 PM
ive done this before just resently... (http://codingforums.com/showthread.php?s=&threadid=15054&highlight=text+file)its not really that simple to do, but it can be done with ie (and netscape but i think you need to do stuff with nodes or something)...
what you do is basically make a hidden div tag, put a iframe into it, with the src of the iframe to the text file, than you read the text with iframename.document.getElementsByTagName('HTML')[0].innerTEXT

Mars
04-30-2003, 08:25 PM
yes but i wan the page viewer to bea lbe to click browse... and open a text file from their computer in to a <select> area......thx for the reply though

kwhubby
05-01-2003, 04:06 AM
oh I think i know what you want... do you want somebody to browse for a text file on there computer and then that text file to get into a <textarea>?? I dont understand what your talking about with the <select> stuff, it sounds really complex there... but If my 1st guess is right than first you make the browse thing <input type="file"> than you make a script so when the file input thing gets the src to the text file, it changes the src of the iframe in the hidden div tag to that file, than it reads that iframe with iframename.document.getElementsByTagName('HTML')[0].innerTEXT
and sends that data to the <textarea> (or the <select>, but I dont understand what you mean by being sent the the option meanue, do you want the text file to form the options... that would be a whole different thing with using regular expresions and such)..... If you want Ill make what I gave instructions for for you, but Im not sure if thats what you want so I didn't make it..

Mars
05-01-2003, 04:14 AM
yes i want each line of the text file to form an option in the <select>....tyvm for replying

example:

Text File:
Mars
Was
Here

Html:

<option>mars</option>
<option>was</option>
<option>here</option>

dang i hope you under stand that jibberish....lol....i cant make myslef ne clearer

kwhubby
05-01-2003, 04:26 AM
ahhhhh crystal clear!!!!! :thumbsup:


:D


oh.... ok this is interesting.... sure... give me a little time Ill make it when I get some time to do it a little later tonight probobly....

Mars
05-01-2003, 04:37 AM
kk cool....can i talk to you on yahoo or msn messengers?

cheesebagpipe
05-01-2003, 05:14 AM
This is real rough, there's a lot involved here. Hopefully it'll give you a start.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>untitled</title>
<style type="text/css">

.space {font: 200 12px arial; color: black; background: orange;}

</style>
<script type="text/javascript" language="javascript">

function makeList(oIframe) {
var textarr = oIframe.document.body.innerHTML.replace(/<\/?xmp>/gi, '').split(/\s+/);
if (textarr == '') return;
var newopt, oSelect = f.dynamic;
oSelect.length = 0;
oSelect.options[0] = new Option('::::::::::::::::::::::::::::::');
oSelect.options[0].className = 'space';
for (var o=0; o<textarr.length; ++o) {
newopt = new Option('::::::::: ' + textarr[o] + ' :::::::::',textarr[o]);
oSelect.options[oSelect.options.length] = newopt;
newopt.className = 'space';
}
}

</script>
</head>
<body bgcolor="olive">
<form name="f" onsubmit="this.action=txtfile.value" target="loader" style="font:bold 22px monospace;color:white;">
&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; <input type="file" name="txtfile"> &amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt;
<input type="submit" value="Next">
<select name="dynamic" id="dynamic" style="width:100px;">
</select><br />
</form>
<iframe name="loader" id="loader" width="0" height="0" onload="makeList(loader)"></iframe>
</body>
</html>


Save a text file, mars.txt:

Mars Was Here

...and, dump it in there....:thumbsup:

Mars
05-01-2003, 05:26 AM
thx a lot.....i may be able ot get it to work

kwhubby
05-01-2003, 05:29 AM
........oh ,..... I see that cheesebridge already beat me to it

Mars
05-01-2003, 06:27 AM
Ok i got this now (thx to you both):

<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function makeList(abcd) {
var textarr = abc.document.getElementsByTagName('HTML')[0].innerText.replace(/<\/?xmp>/gi, '').split(/\s+/);
if (textarr == '') return;
var newopt, oSelect = f.dynamic;
for (var o=0; o<textarr.length; ++o) {
newopt = new Option('' + textarr[o] + '',textarr[o]);
oSelect.options[oSelect.options.length] = newopt;
}
}

</script>
</head>
<body>
<form name="f" onsubmit="this.action=txtfile.value" target="abc" style="font:bold 16px arial;">

<select name="dynamic" id="dynamic" style="width:200px;" multiple="false" size=7>
</select>
<input type="submit" value="Load">
<br>
<input type="file" name="txtfile" size=30>
</form>
<iframe name="abc" id="loader" width="0" height="0" onload="makeList(loader)"></iframe>
</body>
</html>

Now....is it possible to add another <select> box and when the text file is loaded filter different words to a different box.

For example:

Mars - me
Stupid - I
hi - yall

Is the text file. Everything to the left of a dash is sent to the first box and everything to the right of the dash is sent to the second box. and then a copy button for each box which will allow the user to copy the selected item.

I know yall are prolly tired of me by now....so if ya dont feel like messing with it ill undersand....:D .......thx a lot

Mars
05-01-2003, 09:21 PM
nobody can help?...:(

cheesebagpipe
05-02-2003, 09:22 AM
Here's as far as I got...after not being able to figure out why it took two button pushes to submit the form, gave up and went back to work. Maybe you can figure it out. cya

Mars
05-02-2003, 09:32 PM
cool man thanks a lot for ur help

kwhubby
05-02-2003, 11:32 PM
hey mars, for the previous post were u posted the quick edit i did for cheesebridges, were i used abc.document.getElementsByTagName('HTML')[0].innerText.replace(/<\/?xmp>/gi, '').split(/\s+/); since its innerText u dont need the replace part... so use this abc.document.getElementsByTagName('HTML')[0].innerText.split(/\s+/); .... but thats only for that one not for the zip that cheesebridge posted.


and cheesbridge, the way u handled the text file gives me <pre> and </pre> in the selects... thats why i changed it to getElementsByTagName('HTML')[0].innerText so that does not happen

cheesebagpipe
05-03-2003, 02:05 AM
and cheesbridge, the way u handled the text file gives me <pre> and </pre> in the selects... thats why i changed it to getElementsByTagName('HTML')[0].innerText so that does not happenJust change this line:

textarr = IFbody.replace(/(<\/?xmp>|<\/?pre>)/gi, '').replace(/ \- /g, ' ').split(/\s+/);

HTMLElement.innerText is Internet Explorer-only and should be avoided...

Also: change:

var newopt, oSelect1 = f.dynamic1, oSelect2 = f.dynamic2;

to:

var newopt, oSelect1 = document.f.dynamic1, oSelect2 = document.f.dynamic2;

Mars
05-03-2003, 05:18 AM
ok cool thx again man..

Mars
05-05-2003, 05:30 AM
i know i have probably used this way too much but i have one more questionthat is prolly pretty simple for ya...how can i provide a button that will coy the selected option in this:

<select name="dynamic2" id="dynamic2" style="width:100px;" multiple="false">
</select>

I tried this but didnt really know what iw as doing:

<center>
<A href="javascript:HighlightAll('f.dynamic2')">Copy to Clipboard</A>
</center>

it will copy a textbox but not the box i am working with....
thx

EDIT: f is the name of the form