-
Client-Side PerlScript
Hi all. I'm going nuts... I cannot get this thing to work.. Must be doing something wrong but I can't figure out what...
Here's a code example:
Code:
<HTML>
<HEAD>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<TITLE>formtest</TITLE>
</HEAD>
<BODY>
<FORM NAME=myform>
<INPUT TYPE=text NAME=mytest>
<INPUT TYPE=submit>
</FORM>
<SCRIPT LANGUAGE=PerlScript>
$mytest = $window->document->myform->mytest->{'Value'};
$window->document->write("Debug: $mytest\n");
</SCRIPT>
</BODY>
</HTML>
It has no errors. But I cannot seem to get the value of the textfield...
I'm using ActivePerl 5.8, IE6 on WinXP.
-
When the page loads, and the code is parse, the text box is empty.
-
Yes. But when I type something in there and click on submit..
I can see the page reloading with "?mytest=thetextityped" appended..
The textbox shows the typed text but the variable stays empty..
-
Is the text box blank in the second instance? I've never used perlscript so I'm only gandering here, but I would guess that when you initialize the form the second time, its clearing the value. Also, should you be referencing the form or the retained data?
-
I never used perlscript before, but as I'm seeing it, you're never suppling a value for your textfield. That way, the contents of that textfield will always be empty. Try this:
Code:
<HTML>
<HEAD>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<TITLE>formtest</TITLE>
</HEAD>
<BODY>
<FORM NAME="myform" METHOD="GET">
<INPUT TYPE="text" NAME="mytest" VALUE="Foo">
<INPUT TYPE="submit">
</FORM>
<SCRIPT LANGUAGE="PerlScript">
$mytest = $window->document->myform->mytest->{'Value'};
$window->document->write("Debug: $mytest\n");
</SCRIPT>
</BODY>
</HTML>
I can't test it, since I have no activeperl installed. You should get something in your textbox, and that same word should be printed in the body of your page, as far as I see.
By the way: you should also specify that your form uses the GET method to send those variables (<FORM NAME="yadda" METHOD="GET">).
-
Hi
I think, the problem lies in the submit button. The parameters are
sent by GET/POST, which might complicate things unnessarily for
client-side scripting. I would try to replace the submit with an ordinary
button, or just call a PerlScript-function onclick.
Code:
<HTML>
<HEAD>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<TITLE>formtest</TITLE>
</HEAD>
<BODY>
<FORM NAME=myform>
<INPUT TYPE=text NAME=mytest>
<INPUT TYPE=submit onclick="my_perlfunction">
</FORM>
<SCRIPT LANGUAGE=PerlScript>
sub my_perlfunction {
$mytestvalue = $window->document->myform->mytest->{'Value'};
$window->document->write("Debug: $mytestvalue\n");
}
</SCRIPT>
</BODY>
</HTML>
or
Code:
<HTML>
<HEAD>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<TITLE>formtest</TITLE>
</HEAD>
<BODY>
<FORM NAME="myform">
<INPUT TYPE="text" ID="id_mytest">
<INPUT TYPE="button" onclick="my_perlfunction" value="Submit Query">
</FORM>
<SCRIPT LANGUAGE="PerlScript">
sub my_perlfunction {
$testelement = $window->document->getElementById("id_mytest")->value;
$window->document->write("Debug: $testelement\n");
}
</SCRIPT>
</BODY>
</HTML>
I'd go with the ID-version, which is standard (?) for passing values from
one scripting language to another.
Cheers.
-
Thanx guys.. I'm a bit closer now.. removed the submit button (it was for test anyway) and used an <SELECT> in combination with an ID field..
Code:
<HTML>
<HEAD><TITLE>TEST</TITLE>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<SCRIPT LANGUAGE=PerlScript>
sub mychange {
my $aa = $window->document->getElementByID("mytest")->value;
$window->document->write("<h1>Debug: $aa</h1>");
}
</SCRIPT>
</HEAD>
<BODY>
<FORM><SELECT ID=mytest onChange="mychange();">
<OPTION VALUE=1>Choice 1</OPTION>
<OPTION VALUE=2>Choice 2</OPTION>
</SELECT></FORM>
</BODY>
</HTML>
Btw this works too:
Code:
<HTML>
<HEAD><TITLE>TEST</TITLE>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<SCRIPT LANGUAGE=PerlScript>
sub mychange {
my $aa = $window->document->myform->myselect->{'Value'};
$window->document->write("<h1>Debug: $aa</h1>");
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=myform><SELECT NAME=myselect onChange="mychange();">
<OPTION VALUE=1>Choice 1</OPTION>
<OPTION VALUE=2>Choice 2</OPTION>
</SELECT></FORM>
</BODY>
</HTML>
-
Just for kicks here's a variation:
Code:
<HTML>
<HEAD><TITLE>TEST</TITLE>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<SCRIPT LANGUAGE=PerlScript>
sub myform::klik_onClick {
my $aa = $window->document->myform->myselect->{"Value"};
$window->document->write("<h1>klik: $aa</h1>");
}
sub myform::myselect_onChange {
my $aa = $window->document->myform->myselect->{"Value"};
$window->document->write("<h1>change: $aa</h1>");
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME=myform><SELECT NAME=myselect>
<OPTION VALUE=1>Choice 1</OPTION>
<OPTION VALUE=2>Choice 2</OPTION>
</SELECT><INPUT TYPE=button VALUE="Go" NAME=klik></FORM>
</BODY>
</HTML>
This one doesn't seem to like ID's though.. If I try to use ID's the onChange doesn't work anymore. Even if I use NAME and ID fields.. Same values and/or different ones for NAME/ID.
Oh well, got enough to get me going :D