PDA

View Full Version : -------------Perl Help ---------------



vin16
December 5th, 2001, 19:52
Hi Guys,

Help me out.......

This is a jukebox script that I downloaded from a perl archive, the only problem is that the songs selected is sent to the cgi script through a textbox and I want it to be sent via variables instead.

Am attaching the cgi script, hoping for a solution. Please help me out, I tired almost everything with little perl knowledge and so far haven't gotten anyway as mentioned your my last hope.

1.Here's the html file
<html>
<head>
<title>Demo page</title>
</head>

<SCRIPT language=JavaScript>
function LOADJUKEBOX() {
songid = new Array;
count = 0;
for (var i=0;i<document.jukebox.elements.length;i++) {
if (document.jukebox.elements[i].name == 'ID') {
if (document.jukebox.elements[i].checked) {
songid[count] =document.jukebox.elements[i].value;
count ++;
}

}


}
parameters = songid[0];
for (var j=1;j<count;j++) {
parameters += "," +songid[j];
}
url="mytry.cgi?parameter="+parameters;
window.open(url,'PLAYER','toolbar=no,location=no,directories =no,status=yes,menubar=no,resizable=yes,copyhistory=no,scrol lbars=no,width=540,height=230');
}

</SCRIPT>

<SCRIPT language=JavaScript>
function CheckAll()
{
for (var i=0;i<document.jukebox.elements.length;i++)
{
var box = document.jukebox.elements[i];
if (box.name != 'all')
box.checked = 1;
}
javascript:LOADJUKEBOX(this)
}
</SCRIPT>

<SCRIPT language=JavaScript>
function ClearAll()
{
for (var i=0;i<document.jukebox.elements.length;i++)
{
var box = document.jukebox.elements[i];
box.checked = 0;
}
document.jukebox.text1.value = ""
}
</SCRIPT>



<body>


<form action="mytry.cgi" method="POST" name="jukebox">
<table border="0" width="90%" cellspacing="0" cellpadding="0">

<tr>
<td width="100%" bgcolor="#0080FF"><font size="2"><input type="checkbox" name=ID
onclick=javascript:LOADJUKEBOX(this) value="e1">e1 - Star -- Bryan Adams</font></td>
</tr>
<tr>
<td width="100%" bgcolor="#85C47B"><font size="2"><input type="checkbox" name=ID
onclick=javascript:LOADJUKEBOX(this) value="e2">e2 - I will always be right there -- Bryan Adams
</font></td>
</tr>
<tr>
<td width="100%" bgcolor="#0080FF"><font size="2"><input type="checkbox" name=ID
onclick=javascript:LOADJUKEBOX(this) value="e3">e3 - Have you ever loved a woman -- Bryan Adams</font></td>
</tr>
<tr>
<td width="100%" bgcolor="#85C47B"><font size="2"><input type="checkbox" name=ID
onclick=javascript:LOADJUKEBOX(this) value="e4">e4 - Da di da -- Fruit</font></td>
</tr>
<tr>
<td width="100%" bgcolor="#0080FF"><font size="2"><input type="checkbox" name=ID
onclick=javascript:LOADJUKEBOX(this) value="e5">e5 - Get it tonight -- Montell Jordon</font></td>
</tr>

</table>
<p><input type="submit" value="Play your customised list"></p>
<p><input onclick=javascript:ClearAll(this) type="button" value="Clear"></p>
<p><input onclick=javascript:CheckAll(this) type="button" value="Check"></p>
<p><input type="textbox" name="text1" size="-1"></p>
</form>
</body>
</html>


2.Here's the cgi script
#!/usr/bin/perl


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

$Parameter = $in{'Parameter'};

$Songs{'e1'}= "http://members.xoom.com/lewisl818/adam05.rm";
$Songs{'e2'} = "http://members.xoom.com/lewisl818/adam09.rm";
$Songs{'e3'} = "http://members.xoom.com/lewisl818/adam13.rm";
$Songs{'e4'} = "http://members.xoom.com/lewisl818/adam13.rm";
$Songs{'e5'} = "http://members.xoom.com/lewisl818/adam13.rm";

use Time::localtime;

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ s/\n/ /g;# added to strip line breaks

$Parameter = $value;
}

$EXT = ".ram";
$tm = localtime;
($DAY, $MONTH, $YEAR, $HR, $MIN, $SEC) = ($tm->mday, $tm->mon, $tm->year, $tm->hour, $tm->min, $tm->sec);

if($YEAR < 100)
{ $YEAR = $YEAR + 100;
}

# This remove the old .ram files on server

sub song_expire
{
local(@items, $item);
opendir(RAMHANDLE, "./");
@items = grep(/[0-9]$EXT/,readdir(RAMHANDLE));
closedir(RAMHANDLE);
foreach $item (@items)
{
($YMD, $HMS) = split(/_/, $item);

if($YMD != $YEAR.$MONTH.$DAY)
{
unlink("./$item");
}
}
}

&song_expire;


print <<EndHTML1;

<html><head><title>Here comes~</title></head>
<body><center>

EndHTML1

@Lists = split(/,/, $Parameter = $in{'Parameter'})


$FILE_NAME = $YEAR.$MONTH.$DAY. "_" .$HR.$MIN.$SEC.".ram";
$FILE_NAME2 = ">".$FILE_NAME;

open(OUTF,$FILE_NAME2) or &dienice("Couldn't open survey.out for
writing. Please notify webmaster.");

foreach $list (@Lists) {
print OUTF "$Songs{$list}\n";
}

close(OUTF);


# Modify this yourself if you want a different look.. or add a pop up
# windows for the .ram file. If you don't know how to do it. I suggest
# you leave it as it is. =)

print <<EndHTML;


Here is your list<p>
<img src="http://images.real.com/pics/general/freeplayer_g2.gif"><p>

<a href="$FILE_NAME">Click Here to listen!</a><p>
Thanks for visiting my site. =)
<p>
<br>



EndHTML


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

sub dienice {
($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}

Dusty
December 5th, 2001, 22:45
I'm confused by what you're asking. You say it's using a textbox and you want instead to use variables (and what do you mean by that, exactly? JavaScript variables, hidden inputs, variables in the Perl script...?), but in the snippet you gave I saw no textbox, just checkboxes. Is that what you meant, or is that what you want? I'm confused.

vin16
December 6th, 2001, 00:46
Sorry for confusion,

Am attaching, the original files and the first message
contains my version(Changed) .

All I want is for the selected songs stored in a JavaScript variable
on the html file to be sent to the CGI script as a variable.

In the orginial files you can see exactly how the values are passed from the html file to the cgi script using a textbox.

Dusty help me out please,

Here's the original HTML Source Code################
<html>
<head>
<title>Demo page</title>
</head>
<script language="JavaScript">
<!--

<!-- Begin

function addTitle(f)
{
if(f.checked)
{
if(this.document.form1.text1.value == "")
{
this.document.form1.text1.value = f.name;
}
else
{
this.document.form1.text1.value = this.document.form1.text1.value + "," + f.name;
}
}
else
{
if(confirm("This will erase the whole list. Are you sure?"))
{ this.document.form1.text1.value = "";
this.document.form1.reset();
}else
{ f.checked = true;
}
}
}


//-->
</script>


<body>

<form action="original.cgi" method="POST"
name="form1">
<table border="0" width="90%" cellspacing="0" cellpadding="0">

<tr>
<td width="100%" bgcolor="#0080FF"><font size="2"><input type="checkbox" name="e1"
onclick="addTitle(this.form.e1)" value="ON">e1 - Star -- Bryan Adams</font></td>
</tr>
<tr>
<td width="100%" bgcolor="#85C47B"><font size="2"><input type="checkbox" name="e2"
onclick="addTitle(this.form.e2)" value="ON">e2 - I will always be right there -- Bryan Adams
</font></td>
</tr>
<tr>
<td width="100%" bgcolor="#0080FF"><font size="2"><input type="checkbox" name="e3"
onclick="addTitle(this.form.e3)" value="ON">e3 - Have you ever loved a woman -- Bryan Adams</font></td>
</tr>
<tr>
<td width="100%" bgcolor="#85C47B"><font size="2"><input type="checkbox" name="e4"
onclick="addTitle(this.form.e4)" value="ON">e4 - Da di da -- Fruit</font></td>
</tr>
<tr>
<td width="100%" bgcolor="#0080FF"><font size="2"><input type="checkbox" name="e5"
onclick="addTitle(this.form.e5)" value="ON">e5 - Get it tonight -- Montell Jordon</font></td>
</tr>

</table>
<p><input type="submit" value="Play your customised list"></p>
<p><input type="textbox" name="text1" size="50"></p>
</form>
</body>
</html>



Heres the CGI Script#####################
#!/usr/bin/perl

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


$Songs{'e1'} = "http://members.xoom.com/lewisl818/adam05.rm";
$Songs{'e2'} = "http://members.xoom.com/lewisl818/adam09.rm";
$Songs{'e3'} = "http://members.xoom.com/lewisl818/adam13.rm";
$Songs{'e4'} = "http://members.xoom.com/lewisl818/adam13.rm";
$Songs{'e5'} = "http://members.xoom.com/lewisl818/adam13.rm";

use Time::localtime;

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$value =~ s/\n/ /g;# added to strip line breaks

$FORM{$name} = $value;
}

$EXT = ".ram";
$tm = localtime;
($DAY, $MONTH, $YEAR, $HR, $MIN, $SEC) = ($tm->mday, $tm->mon, $tm->year, $tm->hour, $tm->min, $tm->sec);

if($YEAR < 100)
{ $YEAR = $YEAR + 100;
}

# This remove the old .ram files on server

sub song_expire
{
local(@items, $item);
opendir(RAMHANDLE, "./");
@items = grep(/[0-9]$EXT/,readdir(RAMHANDLE));
closedir(RAMHANDLE);
foreach $item (@items)
{
($YMD, $HMS) = split(/_/, $item);

if($YMD != $YEAR.$MONTH.$DAY)
{
unlink("./$item");
}
}
}

&song_expire;


print <<EndHTML1;

<html><head><title>Here comes~</title></head>
<body><center>

EndHTML1

@Lists = split(/,/, $FORM{'text1'});


$FILE_NAME = $YEAR.$MONTH.$DAY. "_" .$HR.$MIN.$SEC.".ram";
$FILE_NAME2 = ">".$FILE_NAME;

open(OUTF,$FILE_NAME2) or &dienice("Couldn't open survey.out for
writing. Please notify webmaster.");

foreach $list (@Lists) {
print OUTF "$Songs{$list}\n";
}

close(OUTF);


# Modify this yourself if you want a different look.. or add a pop up
# windows for the .ram file. If you don't know how to do it. I suggest
# you leave it as it is. =)

print <<EndHTML;


Here is your list<p>
<img src="http://images.real.com/pics/general/freeplayer_g2.gif"><p>

<a href="$FILE_NAME">Click Here to listen!</a><p>
Thanks for visiting my site. =)
<p>
<br>



EndHTML


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

sub dienice {
($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}