Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drop Down Menu from Array txt File

Status
Not open for further replies.

madaxe

Technical User
Dec 21, 2004
44
US
I want to creat a drop down menu from an array can anybody help, i think im on the right track but i cant get it to work. My code below.


Mad Axe


#!/Perl/bin/perl

use CGI;

$userlist = "playerlist.cgi";

print "Content-type: text/html\n\n";

open (PLAYERLIST, "$playerlist");
@userlist = <PLAYERLIST>;
close (PLAYERLIST);


<FORM ACTION='datahandling.cgi' METHOD=POST>
<SELECT NAME="TEST">

foreach $dataline (@userlist){
chomp $dataline;
($value, $description) = split(/\|/, $dataline);

<OPTION VALUE='$value'>$discription</OPTION>

}

</SELECT>
<INPUT TYPE=SUBMIT value="Tell Me">
</FORM>




ARRAY FILE playerlist.cgi



marc1|player1
marc2|player2
marc3|player3
marc4|player4
marc5|player5
marc6|player6
 
That looks correct to me other than you need to print all of the output, not just list it. Also, it doesn't look like you have all of the HTML tags to create a valid page.

Code:
#!/Perl/bin/perl

use CGI;

$userlist = "playerlist.cgi";

print "Content-type: text/html\n\n";

open (PLAYERLIST, "$playerlist");
@userlist = <PLAYERLIST>;
close (PLAYERLIST);

[COLOR=red]print "<html><body>\n";[/color]
print "<FORM ACTION='datahandling.cgi' METHOD=POST>\n<SELECT NAME="TEST">\n";

    foreach $dataline (@userlist){
        chomp $dataline;
        ($value, $description) = split(/\|/, $dataline);

print "<OPTION VALUE='$value'>$discription</OPTION>\n";

}

print "</SELECT>\n<INPUT TYPE=SUBMIT value="Tell Me">\n</FORM>\n";

[COLOR=red]print "</body>\n</html>";[/color]

- Rieekan
 
As Rieekan says - pretty close but just missing the print bits

Also:-

1) You are not making any use of use CGI; - so i've taken it out
2) You will need to escape the double quotation marks in the new print statements - i've added these
3) When you open a file get used to using < or > - for saftey's sake
4) You spelt 'description' as 'discription' in the menu bit

Have tested it and it works fine now

Code:
[b]#!/usr/bin/perl[/b]

$userlist = "playerlist.cgi";

$playerlist = "playerlist.txt";

print "Content-type: text/html\n\n";

open (PLAYERLIST, "$playerlist");
@userlist = <PLAYERLIST>;
close (PLAYERLIST);

print "<html>\n<body>\n";
print "<FORM ACTION='datahandling.cgi' METHOD=POST>\n<SELECT NAME=\"TEST\">\n";

foreach $dataline (@userlist){
  chomp $dataline;
  ($value, $description) = split(/\|/, $dataline);
  print "<OPTION VALUE='$value'>$description</OPTION>\n";
}

print "</SELECT>\n<INPUT TYPE=SUBMIT value=\"Tell Me\">\n</FORM>\n";

print "</body>\n</html>";


Kind Regards
Duncan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top