file listing within an html select box

B

brett

On my website I have a directory filled with possible files that a user can
select to download. Rather than going to the directory itself to download,
I'd like the user to be able to select a single file from a select box
embedded on an html page. I'm looking for a way that I can use javascript to
do this for me. I know that perl could get these filenames for me easily,
but is javascript able to put these into a select box once I have Perl parse
the directory? Is this insecure or is there a better way to do this?

Thanks for your help,
Brett.
 
G

Grant Wagner

brett said:
On my website I have a directory filled with possible files that a
user can
select to download. Rather than going to the directory itself to
download,
I'd like the user to be able to select a single file from a select box
embedded on an html page. I'm looking for a way that I can use
javascript to
do this for me. I know that perl could get these filenames for me
easily,
but is javascript able to put these into a select box once I have Perl
parse
the directory? Is this insecure or is there a better way to do this?

If you are using Perl to parse the directory, use it to populate the
<select> as well. Parsing the list of files using Perl, then using Perl
to output client-side JavaScript that can populate a <select> is an
extra step and places a requirement on your end-user to have JavaScript
enabled.

print "content-type: text/html\n\n";
if (opendir(DIR, "$full_path"))
{
@files = grep(/^[^\.]|[^\.\.]$/, readdir(DIR));
closedir(DIR);

@files = sort(@files);

print "<select name=\"mySelect\">\n";
for $filename (@files)
{
print "<option value=\"" . $filename . "\">" . $filename .
"</option>\n";
}
print "</select>\n";
}
 
B

brett

Grant,

OK, that makes sense and I think the resulting code will be simpler.

Lets just assume for a moment that I am a windows programmer who understands
Perl, but have never added more than a form to a webpage. How would I
include the output of this perl script in my webpage?

Thanks,
Brett.

Grant Wagner said:
brett said:
On my website I have a directory filled with possible files that a
user can
select to download. Rather than going to the directory itself to
download,
I'd like the user to be able to select a single file from a select box
embedded on an html page. I'm looking for a way that I can use
javascript to
do this for me. I know that perl could get these filenames for me
easily,
but is javascript able to put these into a select box once I have Perl
parse
the directory? Is this insecure or is there a better way to do this?

If you are using Perl to parse the directory, use it to populate the
<select> as well. Parsing the list of files using Perl, then using Perl
to output client-side JavaScript that can populate a <select> is an
extra step and places a requirement on your end-user to have JavaScript
enabled.

print "content-type: text/html\n\n";
if (opendir(DIR, "$full_path"))
{
@files = grep(/^[^\.]|[^\.\.]$/, readdir(DIR));
closedir(DIR);

@files = sort(@files);

print "<select name=\"mySelect\">\n";
for $filename (@files)
{
print "<option value=\"" . $filename . "\">" . $filename .
"</option>\n";
}
print "</select>\n";
}
 
J

J. J. Cale

Lets just assume for a moment that I am a windows programmer who
understands
Perl, but have never added more than a form to a webpage. How would I
include the output of this perl script in my webpage?

As Grant explained you do it on the server. No javascript necessary. Grant's
example is pure and simple cgi.
One way is:
Create an html page exactly the way you want the client to see it using a
<select> to display the file names.

Copy everything in the html source up to (but not including) the <select> to
a file. I use something.hdr. Copy the rest of the code after the </select>
and save it as something.ftr.

Now using Grant's code; the first line is the HTTP content type header. MUST
have two linefeeds at the end unless you're outputting other headers(Cookie
or something). Then just one :>}

read the something.hdr file and output it on the fly to stdout
if (opendir(DIR, "$full_path"))
{
@files = grep(/^[^\.]|[^\.\.]$/, readdir(DIR));
closedir(DIR);

@files = sort(@files);

print "<select name=\"mySelect\">\n";
for $filename (@files)
{
print "<option value=\"" . $filename . "\">" . $filename .
"</option>\n";
}
print "</select>\n";
}

read something.ftr to stdout and your done.

This will output your page as a whole to the clients machine.
You can probably get some help from comp.infosystems.www.authoring.cgi.
HTH
Jimbo
 
B

brett

J. J. Cale said:
Lets just assume for a moment that I am a windows programmer who understands
Perl, but have never added more than a form to a webpage. How would I
include the output of this perl script in my webpage?

As Grant explained you do it on the server. No javascript necessary. Grant's
example is pure and simple cgi.
One way is:
Create an html page exactly the way you want the client to see it using a
<select> to display the file names.

Copy everything in the html source up to (but not including) the <select> to
a file. I use something.hdr. Copy the rest of the code after the </select>
and save it as something.ftr.

Now using Grant's code; the first line is the HTTP content type header. MUST
have two linefeeds at the end unless you're outputting other headers(Cookie
or something). Then just one :>}

read the something.hdr file and output it on the fly to stdout
if (opendir(DIR, "$full_path"))
{
@files = grep(/^[^\.]|[^\.\.]$/, readdir(DIR));
closedir(DIR);

@files = sort(@files);

print "<select name=\"mySelect\">\n";
for $filename (@files)
{
print "<option value=\"" . $filename . "\">" . $filename .
"</option>\n";
}
print "</select>\n";
}

read something.ftr to stdout and your done.

This will output your page as a whole to the clients machine.
You can probably get some help from comp.infosystems.www.authoring.cgi.
HTH
Jimbo

It does, but after some research i've realised our webserver allows SSIs, so
i've just added an execute command in there to get it to work.

Brett.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top