12 Replies - 39446 Views - Last Post: 27 September 2005 - 10:40 AM

#1 ft4jnhu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 05

using JavaScript with Perl CGI

Post icon  Posted 21 September 2005 - 07:07 AM

Hello,

I'm very inexperienced at using Perl CGI and at web programming in general. Is there a way to insert Javascript into a Perl CGI program? I would like to use the Javascript alert dialog boxes with my Perl CGI program. I'm simply trying to check my textboxes for valid input before my form is processed and a new web page is brought up. Here is the Javascript code I'm trying to insert into my Perl CGI program --

print "<script LANGUAGE=\"Javascript\">\n";

sub CheckForm_onclick()
{

var myForm = document.form1;

if (myForm.userid.value == "") {

alert("Please enter your user id.");
}

}

print "</SCRIPT>\n";

Assuming that I can get the above code to work, what do I need to put in the code for my submit button so that it calls the CheckForm_onclick() function and then goes to the next web page? This is what I currently have for my submit button --

<input type=\"button\" value=\"Submit Request\" onclick=\"CheckForm_onclick()\"></p>\n";

This is my code to bring up the next web page. How can I get this to mesh with the CheckForm_onclick() function? --

print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\">\n";


Thank you for your help!

Is This A Good Question/Topic? 0
  • +

Replies To: using JavaScript with Perl CGI

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: using JavaScript with Perl CGI

Posted 21 September 2005 - 08:00 AM

You'd just need to modify the form tag, and use the onsubmit event...
print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\" onsubmit=\"CheckForm_onclick()\">\n";


Now, the function will be called when the form is submitted.

A slightly more efficient way would be to have the fucntion return a boolean value indicating that it has passed the validation, and it is ok to submit...like so:
sub CheckForm_onclick()
{
var message = "";
var myForm = document.form1;

if (myForm.userid.value == "") {
   message = "Please enter your id\n";
}
if(message!="")
{
   return(false);
}
else{
   return(true);
}


}


and change the form tag to
print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\" onsubmit=\"return(CheckForm_onclick())\">\n";


this will tell the fucntion to submit the form if and aonly if the userid field has been completed.
Was This Post Helpful? 0
  • +
  • -

#3 ft4jnhu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 05

Re: using JavaScript with Perl CGI

Posted 22 September 2005 - 06:05 AM

Thanks, Amadeus, but I'm still having problems getting my program to execute the Checkform_onclick() function. Maybe it will help if I just show you my whole program. I've tried both of the <FORM ACTION tags that you suggested, but the program still goes straight to message.pl without executing the CheckForm_onclick() function. I also tried your message code, but I can't get it or my alert box to execute. --

#!/usr/local/bin/perl

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

#use strict;
use CGI;
$query = new CGI;


print "<html>\n";
print "<head>\n";

print "<LANGUAGE=Javascript>\n";

sub CheckForm_onclick()
{

#print "GOT HERE!!";
var message = "";
var myForm = document.form1;

if (myForm.userid.value == "" && myForm.num_tickets.value == "") {

alert("Please enter your user id.");
#message = "Please enter your user id\n";
}

#if (message != "") {

#return(false);
#}
#else {

#return(true);
#}
}

print "</SCRIPT>\n";

&print_form;

sub print_form {

print "<title>Ticket Purchases for Special Events</title>\n";
print "</head>\n";

print "<body>\n";

print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\" onsubmit=\"CheckForm_onclick()\">\n";

#print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\" onsubmit=\"return (CheckForm_onclick())\">\n";


print "<table><td><p>User ID: ";
print "<input type=\"text\" name=\"userid\" value=\"$userid\" size=\"7\">\n";
print "</p></td></table>";

print "<p><input type=\"submit\" value=\"Submit Request\"></p>\n";

print "</form>\n";

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

}
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: using JavaScript with Perl CGI

Posted 22 September 2005 - 06:36 AM

Did you get to the GOT HERE line at the beginning of the function? Also, my error, the code I provided should read
ub CheckForm_onclick()
{
var message = "";
var myForm = document.form1;

if (myForm.userid.value == "") {
  message = "Please enter your id\n";
}
if(message!="")
{
  alert(message);
  return(false);
}
else{
  return(true);
}


}


Was This Post Helpful? 0
  • +
  • -

#5 ft4jnhu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 05

Re: using JavaScript with Perl CGI

Posted 22 September 2005 - 09:26 AM

Amadeus,

No, "Got Here" does not appear, the program just goes straight to message.pl. It doesn't matter which <FORM ACTION> I use.

Thanks,
ft4jnhu
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: using JavaScript with Perl CGI

Posted 22 September 2005 - 04:49 PM

%$#$%&*!!@

Sorry, my friend, it was staring me in the face, I just could not see the forest for the trees...you are specifying a javascript sub procedure, but we are calling it like a function...try the following (simplified):
#!/usr/local/bin/perl

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

#use strict;
use CGI;
$query = new CGI;


print "<html>\n";
print "<head>\n";

print "<LANGUAGE=Javascript>\n";
print "<script>\n";
print "function CheckForm_onclick()\n";
print "{\n";
print "var myForm = document.form1;\n";
print "var message = \"\";\n";
print "if(myForm.userid.value == \"\") {\n";
print "message = \"Please enter your user id\";\n";
print "}\n";
print "if(message!=\"\"){\n";
print "alert(message);\n";
print "return(false);\n";
print "}\n";
print "else{\n";
print "return(true);\n";
print "}\n";
print "}\n";

print "</SCRIPT>\n";

&print_form;

sub print_form {

print "<title>Ticket Purchases for Special Events</title>\n";
print "</head>\n";

print "<body>\n";

#print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\" onsubmit=\"CheckForm_onclick()\">\n";

print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\" onsubmit=\"return (CheckForm_onclick())\">\n";


print "<table><td><p>User ID: ";
print "<input type=\"text\" name=\"userid\" value=\"$userid\" size=\"7\">\n";
print "</p></td></table>";

print "<p><input type=\"submit\" value=\"Submit Request\"></p>\n";

print "</form>\n";

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

}


Note the change in the top line of the javascript, from sub to function.
Was This Post Helpful? 0
  • +
  • -

#7 ft4jnhu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 05

Re: using JavaScript with Perl CGI

Posted 24 September 2005 - 07:33 AM

Amadeus,

Sorry, this doesn't appear to be working, either. I copied your code, below is what I currently have. When I click the Submit button without entering a user id, the program still goes straight to message.pl without calling CheckForm_onclick(). Also, my "GOT HERE!!" message never appears.

Thank you for your valiant efforts,
ft4jnhu --


#!/usr/local/bin/perl

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

#use strict;
use CGI;
$query = new CGI;

print "<html>\n";
print "<head>\n";

print "<LANGUAGE=Javascript>\n";
print "<script>\n";
print "function CheckForm_onclick()\n";
print "{\n";

print "GOT HERE!!!!!!!!!!!!!\n";

print "var myForm = document.form1;\n";
print "var message = \"\";\n";

print "if (myForm.userid.value == \"\") {\n";
print "message = \"Please enter your user id.\"\n";
print "}\n";

print "if (message != \"\") {\n";
print "alert(message);\n";
print "return(false);\n";
print "}\n";
print "else {\n";
print "return(true);\n";
print "}\n";
print "}\n";

print "</SCRIPT>\n";

&print_form;

sub print_form {

print "<title>Ticket Purchases for Special Events</title>\n";
print "</head>\n";

print "<body>\n";

#print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\"
#onsubmit=\"CheckForm_onclick()\">\n";

print "<FORM action=\"message.pl\" method=\"post\" name=\"form1\" onsubmit=\"return (CheckForm_onclick())\">\n";

print "<table><td><p>User ID: ";
print "<input type=\"text\" name=\"userid\" value=\"$userid\" size=\"7\">\n";
print "</p></td></table>";

print "<p> &<input type=\"submit\" value=\"Submit Request\"></p>\n";

print "</form>\n";

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


}
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: using JavaScript with Perl CGI

Posted 24 September 2005 - 08:17 AM

Hmmm....I'm not sure what to tell you...the script I provided works perfectly for me...as for the GOT HERE line, it may not be working because it is a PERL command within a javascript function. Try removing that completely.

I hate to ask, but do you have javascript enabled on your browser?

I'll take a look again later when I get back, but that script does work for me...
Was This Post Helpful? 0
  • +
  • -

#9 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: using JavaScript with Perl CGI

Posted 24 September 2005 - 01:04 PM

OK, I retested the script I gave you, and it worked for me...I took the newest scrip that you provided (I took out the GOT HERE line) and that worked for me as well. This tells me it may be something about the setup. What OS and browser are you using? On that OS, what webserver software are you using?
Was This Post Helpful? 0
  • +
  • -

#10 ft4jnhu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 05

Re: using JavaScript with Perl CGI

Posted 26 September 2005 - 06:45 AM

Amadeus,

When I run the Javascript code as a .html file in a non-cgi-bin directory (without using Perl CGI), the code does work. However, I've been trying to run your code in a cgi-bin directory as a .perl file, and that doesn't work. If I try to run the .perl file in a non-cgi-bin directory, I get an error in my browser. My web administrator told me that my Perl CGI code can't be run outside of the cgi-bin directory. I haven't asked him yet if Javascript can be run in the cgi-bin directory, but it hasn't worked thus far.

Do you know of any reason why Javascript can't be run in a cgi-bin directory?

Thanks,
ft4jnhu
Was This Post Helpful? 0
  • +
  • -

#11 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: using JavaScript with Perl CGI

Posted 26 September 2005 - 11:47 AM

I honestly don't...I was running the code locally, no cgi-bin directory, just a folder on my machine, and accessed through the browser. Ask your host of there's something stopping it from running...and perhaps you can try just including the javascript in a separate file, although that should not make a difference...
Was This Post Helpful? 0
  • +
  • -

#12 ft4jnhu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 20-September 05

Re: using JavaScript with Perl CGI

Posted 27 September 2005 - 10:38 AM

Amadeus,

It's working. The problem was that stupid "Got Here!!" statement, the Javascript didn't know what to do with it. Thanks again for all of your help, I'll probably need you in the future!!

ft4jnhu
Was This Post Helpful? 0
  • +
  • -

#13 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,507
  • Joined: 12-July 02

Re: using JavaScript with Perl CGI

Posted 27 September 2005 - 10:40 AM

No problem, friend...glad you got it working.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1