Javascript Form Validation Issue

N

Nigel

Hello,

I am creating a Perl CGI page with Javascript which I believe has a
problem with the javascript.

Below is my form which has two submit buttons with two different
actions.
I am having an issue with my "Submit FAQ" button. The "Submit FAQ"
button calls the javascript function validate(). This function is
supposed to check to make sure the 'content' field is populated. If
the field is populated it will go to the action page called
"addFAQAction.pl". If the 'content' field in undefined a message will
be displayed to the user stating to populate the 'content' field.
Currently the functionality does not catch when the 'content' field is
blank and just continues to the action page.

Can anyone see what I might be doing wrong which causes my form not to
catch when 'content' is undefined?

Thanks in Advance.
Nigel


#!/usr/bin/perl -w
use strict;
use CGI;
my $content = param('content');

print <<"HTML";
<HTML>
<HEAD>
<SCRIPT language='javascript'>

function checkContent (strng) {
var error = "";
if (strng == ""){
error = "Please enter some content for this FAQ.\n";
}
return error;
}


function validate()
{
var why = "";
why += checkContent(addFAQ.content.value);
if (why != ""){
alert(why);
return false;
}
else{
document.addFAQ.submit();
return true;
}
}
</SCRIPT>
</HEAD>
<BODY>
<form name='addFAQ' id='addFAQ' action='addFAQAction.pl' method='post'>
<table>
<tr>
<td>FAQ Content:<br></td>
HTML
<td><textarea id='content' name='content' cols='70'
rows='20'>$content</textarea></td>
print <<"HTML";
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="update" value="Update Page"
onClick=\"document.addFAQ.action='addFAQ.pl'\">
<input type="submit" name="Submit" value="Submit FAQ"
onSubmit='javascript:validate();'>
</td>
</tr>
</table>
</form>
</BODY>
</HTML>
HTML
 
W

web.dev

Nigel said:
<SCRIPT language='javascript'>

The language attribute is deprecated, use the type attribute instead:

<form name='addFAQ' id='addFAQ' action='addFAQAction.pl' method='post'> [snip]
<input type="submit" name="update" value="Update Page"
onClick=\"document.addFAQ.action='addFAQ.pl'\">
<input type="submit" name="Submit" value="Submit FAQ"
onSubmit='javascript:validate();'>

1. When you "update the page", you are still submitting the page.
2. The input element does not have an onsubmit event handler, the form
element does.

Personally, I discourage the use of multiple submit buttons, as it
usually seems unnecessary. You could for example, use a checkbox to
indicate you're "updating a page" instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top