For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > March 2005 > Problem writing JavaScript method with CGI.pm









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Problem writing JavaScript method with CGI.pm
Tyson Sommer

2005-03-03, 8:55 pm

I am trying to use CGI.pm to build a button for a form that has an onClick
method defined that's supposed to make the page reload itself. Here's the
code I'm using (assume strictures and warnings, and that $q = new CGI;):


Current working code:
---------------------


sub tt_write_form () {

# Write the omnipresent parts of the form
print $q->start_form( # <-- print statement one
-method => 'POST',
-name => 'my_form'
);

# Write the reload button
$q->{'escape'} = 0; ### Tell CGI.pm not to escape the HTML so we can
preserve the quotes first
print $q->button( # <-- print statement two
-name => 'reload',
-value => 'Reload?',
-onclick => "location.href='$self'"
);
$q->{'escape'} = 1; ### Tell CGI.pm to start escaping again

# Move on with the form, with HTML escaping back on...
print $q->p, # <-- print statement three
$q->b( "Task:" ),
$q->br,
$q->popup_menu(
-name => 'task',
-onchange => 'my_form.submit',
-values => ['blank','cfg','loc','inv','bw'],
);

}
-------------------


Okay, what I'd like to do is tighten that up a bit. I'd like to put it all
inside of one print; statement, but I don't want to have to tell CGI.pm to
turn off HTML escaping for the whole form. I just want to do it for the one
onClick tag in the button. If I don't turn off HTML escaping for that, then
CGI.pm generates an onclick method to the button that says:

onclick="location.href='me.cgi'"

When I want it to say

onclick="location.href='me.cgi'"

If you look above, I have one print; statement for the beginning of the
form, then I turn escaping off (see first ### above), then I print the
button, then I turn escaping back on (2nd ### above), then I go on with my
form with additional print;s. Is there a way to switch HTML escaping off for
just one tag within the CGI method syntax? See ### below:



What I'd like to do:
--------------------

sub tt_write_form () {

# Write the omnipresent parts of the form
print $q->start_form( # <-- only print statement for this whole
subroutine
-method => 'POST',
-name => 'my_form'
),

# Write the reload button
$q->button(
-name => 'reload',
-value => 'Reload?',
-onclick => 'location.href='$self'' ### <-- somehow set
$q->{'escape'} = 0 JUST for this line???
),

# Move on with the form, with HTML escaping back on (somehow)...
$q->p,
$q->b( "Task:" ),
$q->br,
$q->popup_menu(
-name => 'task',
-onchange => 'my_form.submit',
-values => ['blank','cfg','loc','inv','bw']
); # <-- end of only print statment

}
--------------------



***OR*** is there any way to trick CGI.pm into thinking this particular
instance of -onclick isn't the same as the one defined within the module,
while still having it return the correct HTML? (CGI.pm only sends the -name,
-value, and -onclicks to the escapeHTML() routine, and therefore I wouldn't
have to unescape at all).


Thanks!
Tyson


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com