View Full Version : form submission and perl variables

06-09-2008, 11:02 PM
In a form, how do you get access to a perl variable (for example $error_message) in my javascript code encoded in the same html page?

Just saying "$error_message" doesn't work. The only way I currently know how is by creating a hidden field in my form and setting the value to be $error_message:

print "<input type=\"hidden\" name=\"error_message\" value=\"$error_message\" />\n";

and then passing it as a parameter to the javascript code:

print "<input type=\"button\" name = \"button\" value=\"Log in\"
onclick=\"somefunction(myForm.error_message.value)\" />\n";

My javascript code chooses betweeen two different places to go to depending on the value of $error_message like so:

print "<SCRIPT language=\"javascript\" >\n";
print " function somefunction(fd)\n";
print "{\n";
print "alert(fd)\n";
print "if (fd==''){\n";
print "document.invisible.submit()}\n";
print "else {document.myForm.submit()}\n";
print "}\n";
print "</SCRIPT>\n";

However, I have found out that the value of my error_message variable is one step behind i.e. if I am at step 2, it still has the value of step 1; and if I am at step 3 it still is at step 2 etc.

This results in me having to hit the submit button twice before the form is submitted.

I also tried: document.getElementById('error_message').value but it is still out of step by 1.