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]
# 1

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

evnafetsa at 2007-7-13 2:51:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Try intead of using <html:button>,<html:submit>... I think it will work for u...
nokia6030a at 2007-7-13 2:51:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
Hi,Solns1 . instead of submit btn use Normal Btn2. Instead of document.forms['addUserForm'].submit();use addUserForm.submit(); Ganesh
GaneshNarayana at 2007-7-13 2:51:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thanks guys for all your help. The last solution seemed to be the one that worked. Instead of document.forms['addUserForm'].submit(), I used addUserForm.submit() and it worked.Again , thank you all for the possible suggestions to solve the problem.
gtataa at 2007-7-13 2:51:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
HiCould please explain me how to use <html:button> to submit form.i dont know how to specify form name in struts for submitting the form?Thanks
snehascreena at 2007-7-13 2:51:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

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

rvijaya at 2007-7-13 2:51:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...