Submit Struts Form with JavaScript
I have a JSP page with java script and form as shown below. When I click on the button, the javascript alerts the message "About to submit form" but the form is never submitted. Any ideas why?
Thanks.
<script type="text/javascript">
function validateTheForm ()
{
alert("About to submit form");
document.forms['addUserForm'].submit();
}
</script>
<html:form action="AddUser.do?do=addUser" enctype="multipart/form-data">
<table border=0>
<tr>
<td align=right><strong><font size="2" face="Verdana"><bean:message key="adduser.label.userId"/>:</font></strong></td>
<td align=left><html:text property="userId" maxlength="20"/></td>
<td><html:errors property="userId"/></td>
</tr>
</table>
<html:button property="submit" onclick="javascript:validateTheForm();">Submit</html:button>
</html:form>
[1047 byte] By [
gtataa] at [2007-10-2 10:42:43]

The standard reason would be javascript error.
is the name of the form correct?
try this:
alert(document.forms['addUserForm']);
if it doesn't come up with [OBJECT] then check the name of the form (view source on the generated HTML)
Most probably you have it correct, but it pays to check.
The real reason it is not submitting, is that you have overridden the method submit with a button control.
<html:button property="submit" ...
This generates code like ><input type="button" name="submit"/>
so when you access document.forms['addUserForm'].submit it is accessing the button, rather than the method.
The solution: rename your property to something other than submit.
(and don't call it action either ;-))
Does your html:button really need a property?
Good luck,
evnafets
Hi,
Try the following code for using <html:button> & submitting a form value in struts....
in onclick event call a javascript function with form name as parameter......
More over u can specify form name as
<html:form action="<action-value>" styleId="form name">
<html:button value="Add" onclick=validate(<form name>)/>
java script function
function validate(<form name>){
var name = document.getElementById(<form name>);
name.submit();
}
replace the <form name> with ur specified form name......
try this code & tell me if this works..... i'm using this method..... if there is any error or wrong in my method kindly inform me...
regards,
R Vijay