Passing CGI parameters to Javascript 
Author Message
 Passing CGI parameters to Javascript

I'm using CGI.pm to generate a form.  On submit, I would like to
validate the contents of the form using Javascript.  The code below is
generating an Object Expected error on the followng line: if
(isBlank(theForm.DaytimePhoneNumber.value)).  I think it has something
to do with how the form name is being created using CGI.pm.  I'm just
not sure how to reference this parameter in my Javascript.  Anyone
have any ideas?

Thanks,
Suzanne

#!/usr/local/bin/perl
#Required - Link to CGI.pm module and create new instance of the CGI
object
use CGI qw(:standard);

# The form has already been filled out, so display thank you page
if (param("First_Name")|| param("Last_Name") || param("Phone_Number")
|| param("Email") || param("Question")){ # the form has already been
filled out
        $firstname = param("First_Name");
        $lastname = param("Last_Name");
        $phone = param("DaytimePhoneNumber");
        $email = param("Email");
        $question = param("Question");
        print header, start_html(-BGCOLOR=>'#333366');
        print "<font face='arial' color='white' size='2'><h4>";
        print ("Thank you. A DeMesy representative will be contacting you
shortly.");
        print "</font>";
        print end_html;

        #Declare variables

        $sender = "DeMesy Fine Watches";
        $subject = "I have a question about a product on your site...";
        $subjectConfirm = "Your email has been sent to DeMesy Fine Watches";

#Path to sendmail program
$mailer = "/usr/vde/bin/sendmail";

#Open filehandle to send email
open (MAIL, "| $mailer -t") || die ("$!\n");

#Email to DeMesy
print MAIL <<CONFIRM;
From:  $email
To: DeMesy Fine Watches <$recipient>
Subject: $subject
The following was submitted from the Questions form:
First Name: $firstname
Last Name: $lastname
Email: $email
Daytime Phone Number: $phone
Product Number: $productcode
Question: $question
CONFIRM

close(MAIL);

#Confirmation Email to Sender
open (MAIL, "| $mailer -t") or die ("$!\n");

print MAIL <<CONFIRM_SEND;
From:  DeMesy Fine Watches <$recipient>
To: $firstname $lastname <$email>
Subject: $subjectConfirm
Your email has been submitted.  A DeMesy representative will contact
you shortly.
CONFIRM_SEND
close(MAIL);

# First time through so present clean form

Quote:
} else {

        $productcode = param("Product_Code");
        $questiondefault = "Please enter your question about product number
$productcode.";
$JSCRIPT=<<END;
      function verify(theForm) {
                        var msg="";
                        var error=false;
                        var phone="

                        if (isBlank(theForm.DaytimePhoneNumber.value)){
                                error = true; msg = msg + "-Phone is blank.";
                        }              
                        if (isBlank(theForm.Email.value)){
                                error = true;
                                msg = msg + "-Email is blank.";
                        }                      
                        if (error){
                        alert("Please correct the following errors:" + msg);
                                return false;
                        } else {
                                return true;
                        }
                }  
END
        print header;  
        print start_html(-BGCOLOR=>'#333366',-script=>$JSCRIPT);
        print "<font face='arial' color='white' size='2'>";
        print start_form(-name=>'theForm',-onsubmit=>'return
verify(theForm);');
        print ("First Name: ", br);
        print (textfield("First_Name","",25,""), p);
        print ("Last Name: ", br);
        print (textfield("Last_Name","",25,""), p);
        print ("Email Address: ", br);
        print (textfield("Email","",35,""), p);
        print ("Daytime Phone Number: ", br);
        print (textfield("DaytimePhoneNumber","",20,""), p);
        print ("Question: ", br);
        print (textarea("Question", $questiondefault,7,40));
        print p(submit("Submit"), reset("Clear Form"));
        print end_form();
        print "</font>";
        print end_html;

Quote:
}



Wed, 21 Apr 2004 01:12:49 GMT  
 Passing CGI parameters to Javascript
WOW! okay first rule of thumb always use strict and also set the warn option
in your perl call
aka
#!/usr/local/bin/perl -w
use strict;
we will overlook that for right now.
along with that when use(ing) strict you need to formally declare all of you
variables so make sure to add my in front of all variable declarations
aka
my $firstname = param("First_Name");

The main problems with this is not the perl at all but the JavaScript:
First of all you needed a isBlank function
Second of all It's easier to send the full object to a isBlank function than
just it's value
so here is the new JavaScript Code Segment along with some other minor
changes:

$JSCRIPT=<<END;
function verify(theForm) {
var msg="";
var error=false;
var phone="";

if (isBlank(theForm.DaytimePhoneNumber)){
error = true; msg = msg + "-Phone is blank.";

Quote:
}

if (isBlank(theForm.Email)){
error = true;
msg = msg + "-Email is blank.";
Quote:
}

if (error){
alert("Please correct the following errors:" + msg);
return false;
Quote:
} else {
return true;
}
}

function isBlank(elm){
 if (elm.value == "" ||
     elm.value == null)
 return true;
 else return false;
Quote:
}

END

Next if your going to use the shorthand function calls of CGI.pm don't
include any variable piece your not going to use such as:
print (textfield("First_Name","",25,""), p);
should simply be:
print (textfield("First_Name","",25), p);

Perl unlike other languages doesnot care if you don't supply a fuction
parameter like that and CGI.pm will add extra html code to your output if
you do.

When those changes are made there are no problems with this code.
Jonathan Clover
NATI

P.S. I will include the full new code down below for you.

#!/usr/local/bin/perl -w
#Required - Link to CGI.pm module and create new instance of the CGI object
use CGI qw(:standard);

# The form has already been filled out, so display thank you page
if (param("First_Name")|| param("Last_Name") || param("Phone_Number") ||
param("Email") || param("Question")){ # the form has already beenfilled out
my $firstname = param("First_Name");
my $lastname = param("Last_Name");
my $phone = param("DaytimePhoneNumber");
my $email = param("Email");
my $question = param("Question");
print header, start_html(-BGCOLOR=>'#333366');
print "<font face='arial' color='white' size='2'><h4>";
print "Thank you. A DeMesy representative will be contacting youshortly.";
print "</font>";
print end_html;

#Declare variables

my $sender = "DeMesy Fine Watches";
my $subject = "I have a question about a product on your site...";
my $subjectConfirm = "Your email has been sent to DeMesy Fine Watches";

#Path to sendmail program
my $mailer = "/usr/vde/bin/sendmail";

#Open filehandle to send email
open (MAIL, "| $mailer -t") || die ("$!\n");

#Email to DeMesy
print MAIL <<CONFIRM;
From:  $email
To: DeMesy Fine Watches <$recipient>
Subject: $subject
The following was submitted from the Questions form:
First Name: $firstname
Last Name: $lastname
Email: $email
Daytime Phone Number: $phone
Product Number: $productcode
Question: $question
CONFIRM

close(MAIL);

#Confirmation Email to Sender
open (MAIL, "| $mailer -t") or die ("$!\n");

print MAIL <<CONFIRM_SEND;
From:  DeMesy Fine Watches <$recipient>
To: $firstname $lastname <$email>
Subject: $subjectConfirm
Your email has been submitted.  A DeMesy representative will contact
you shortly.
CONFIRM_SEND
close(MAIL);

# First time through so present clean form

Quote:
} else {

my $productcode = param("Product_Code");
my $questiondefault = "Please enter your question about product number
$productcode.";
my $JSCRIPT=<<END;
function verify(theForm) {
var msg="";
var error=false;
var phone="";

if (isBlank(theForm.DaytimePhoneNumber)){
error = true; msg = msg + "-Phone is blank.";

Quote:
}

if (isBlank(theForm.Email)){
error = true;
msg = msg + "-Email is blank.";
Quote:
}

if (error){
alert("Please correct the following errors:" + msg);
return false;
Quote:
} else {
return true;
}
}

function isBlank(elm){
 if (elm.value == "" ||
     elm.value == null)
 return true;
 else return false;
Quote:
}

END
print header;
print start_html(-BGCOLOR=>'#333366',-script=>$JSCRIPT);
print "<font face='arial' color='white' size='2'>";
print start_form(-name=>'theForm',-onsubmit=>'return verify(this);');
print ("First Name: ", br);
print (textfield("First_Name","",25), p);
print ("Last Name: ", br);
print (textfield("Last_Name","",25), p);
print ("Email Address: ", br);
print (textfield("Email","",35), p);
print ("Daytime Phone Number: ", br);
print (textfield("DaytimePhoneNumber","",20), p);
print ("Question: ", br);
print (textarea("Question", $questiondefault,7,40));
print p(submit("Submit"), reset("Clear Form"));
print end_form();
print "</font>";
print end_html;
Quote:
}



Wed, 21 Apr 2004 08:41:48 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. passing parameter into JavaScript

2. passing javascript vars to CGI

3. passing javascript var in perl cgi

4. Passing a Javascript cookie to a Perl/CGI script

5. Passing Javascript variables to Perl CGI?

6. passing parameters from URL to perl CGI script

7. CGI Parameter passing with spaces

8. passing parameters between perl cgi scripts

9. passing parameters to CGI

10. Passing Command Line Parameters cgi file

11. passing parameters between perl cgi scripts

12. CGI string parameter passing

 

 
Powered by phpBB® Forum Software